date.js 2.6 KB

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