date.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var dateUtils = (function (exports) {
  2. 'use strict';
  3. function stripTime(timeValue) {
  4. return new Date(timeValue).setHours(0, 0, 0, 0);
  5. }
  6. function today() {
  7. return new Date().setHours(0, 0, 0, 0);
  8. }
  9. // Get the time value of the start of given date or year, month and day
  10. function dateValue(...args) {
  11. switch (args.length) {
  12. case 0:
  13. return today();
  14. case 1:
  15. return stripTime(args[0]);
  16. }
  17. // use setFullYear() to keep 2-digit year from being mapped to 1900-1999
  18. const newDate = new Date(0);
  19. newDate.setFullYear(...args);
  20. return newDate.setHours(0, 0, 0, 0);
  21. }
  22. function addDays(date, amount) {
  23. const newDate = new Date(date);
  24. return newDate.setDate(newDate.getDate() + amount);
  25. }
  26. function addWeeks(date, amount) {
  27. return addDays(date, amount * 7);
  28. }
  29. function addMonths(date, amount) {
  30. // If the day of the date is not in the new month, the last day of the new
  31. // month will be returned. e.g. Jan 31 + 1 month → Feb 28 (not Mar 03)
  32. const newDate = new Date(date);
  33. const monthsToSet = newDate.getMonth() + amount;
  34. let expectedMonth = monthsToSet % 12;
  35. if (expectedMonth < 0) {
  36. expectedMonth += 12;
  37. }
  38. const time = newDate.setMonth(monthsToSet);
  39. return newDate.getMonth() !== expectedMonth ? newDate.setDate(0) : time;
  40. }
  41. function addYears(date, amount) {
  42. // If the date is Feb 29 and the new year is not a leap year, Feb 28 of the
  43. // new year will be returned.
  44. const newDate = new Date(date);
  45. const expectedMonth = newDate.getMonth();
  46. const time = newDate.setFullYear(newDate.getFullYear() + amount);
  47. return expectedMonth === 1 && newDate.getMonth() === 2 ? newDate.setDate(0) : time;
  48. }
  49. // Calculate the distance bettwen 2 days of the week
  50. function dayDiff(day, from) {
  51. return (day - from + 7) % 7;
  52. }
  53. // Get the date of the specified day of the week of given base date
  54. function dayOfTheWeekOf(baseDate, dayOfWeek, weekStart = 0) {
  55. const baseDay = new Date(baseDate).getDay();
  56. return addDays(baseDate, dayDiff(dayOfWeek, weekStart) - dayDiff(baseDay, weekStart));
  57. }
  58. // Get the ISO week of a date
  59. function getWeek(date) {
  60. // start of ISO week is Monday
  61. const thuOfTheWeek = dayOfTheWeekOf(date, 4, 1);
  62. // 1st week == the week where the 4th of January is in
  63. const firstThu = dayOfTheWeekOf(new Date(thuOfTheWeek).setMonth(0, 4), 4, 1);
  64. return Math.round((thuOfTheWeek - firstThu) / 604800000) + 1;
  65. }
  66. // Get the start year of the period of years that includes given date
  67. // years: length of the year period
  68. function startOfYearPeriod(date, years) {
  69. /* @see https://en.wikipedia.org/wiki/Year_zero#ISO_8601 */
  70. const year = new Date(date).getFullYear();
  71. return Math.floor(year / years) * years;
  72. }
  73. exports.addDays = addDays;
  74. exports.addMonths = addMonths;
  75. exports.addWeeks = addWeeks;
  76. exports.addYears = addYears;
  77. exports.dateValue = dateValue;
  78. exports.dayOfTheWeekOf = dayOfTheWeekOf;
  79. exports.getWeek = getWeek;
  80. exports.startOfYearPeriod = startOfYearPeriod;
  81. exports.stripTime = stripTime;
  82. exports.today = today;
  83. Object.defineProperty(exports, '__esModule', { value: true });
  84. return exports;
  85. }({}));