Get + Set

Millisecond / Second / Minute / Hour

Get the Millisecond/Second/Minute/Hour of the given date.

  1. // Moment.js
  2. moment().seconds();
  3. // => 49
  4. moment().hours();
  5. // => 19
  6. // Native
  7. new Date().getSeconds();
  8. // => 49
  9. new Date().getHours();
  10. // => 19
  11. // date-fns
  12. import getSeconds from 'date-fns/getSeconds';
  13. import getHours from 'date-fns/getHours';
  14. getSeconds(new Date());
  15. // => 49
  16. getHours(new Date());
  17. // => 19
  18. // dayjs
  19. dayjs().second();
  20. // => 49
  21. dayjs().hour();
  22. // => 19
  23. // Luxon
  24. DateTime.local().second;
  25. // => 49
  26. DateTime.local().hour;
  27. // => 19

Performance tests

Library Time
Moment 1432.647ms
Native 367.769ms
DateFns 608.878ms
DayJs 502.302ms
Luxon 1296.507ms

Set the Millisecond/Second/Minute/Hour of the given date.

  1. // Moment.js
  2. moment().seconds(30);
  3. // => "2018-09-09T09:12:30.695Z"
  4. moment().hours(13);
  5. // => "2018-09-09T03:12:49.695Z"
  6. // Native
  7. new Date(new Date().setSeconds(30));
  8. // => "2018-09-09T09:12:30.695Z"
  9. new Date(new Date().setHours(13));
  10. // => "2018-09-09T03:12:49.695Z"
  11. // date-fns
  12. import setSeconds from 'date-fns/setSeconds';
  13. import setHours from 'date-fns/setHours';
  14. setSeconds(new Date(), 30);
  15. // => "2018-09-09T09:12:30.695Z"
  16. setHours(new Date(), 13);
  17. // => "2018-09-09T03:12:49.695Z"
  18. // dayjs
  19. dayjs().set('second', 30);
  20. // => "2018-09-09T09:12:30.695Z"
  21. dayjs().set('hour', 13);
  22. // => "2018-09-09T03:12:49.695Z"
  23. // luxon
  24. DateTime.utc()
  25. .set({ second: 30 })
  26. .toJSDate();
  27. // => "2018-09-09T09:12:30.695Z"
  28. DateTime.utc()
  29. .set({ hour: 13 })
  30. .toJSDate();
  31. // => "2018-09-09T03:12:49.695Z"

Performance tests

Library Time
Moment 1690.007ms
Native 636.242
DateFns 696.768ms
DayJs 1891.058ms
Luxon 8630.314ms

Date of Month

Gets or sets the day of the month.

  1. // Moment.js
  2. moment().date();
  3. // => 9
  4. moment().date(4);
  5. // => "2018-09-04T09:12:49.695Z"
  6. // Native
  7. new Date().getDate();
  8. // => 9
  9. new Date().setDate(4);
  10. // => "2018-09-04T09:12:49.695Z"
  11. // date-fns
  12. import getDate from 'date-fns/getDate';
  13. import setDate from 'date-fns/setDate';
  14. getDate(new Date());
  15. // => 9
  16. setDate(new Date(), 4);
  17. // => "2018-09-04T09:12:49.695Z"
  18. // dayjs
  19. dayjs().date();
  20. // => 9
  21. dayjs().set('date', 4);
  22. // => "2018-09-04T09:12:49.695Z"
  23. // luxon
  24. DateTime.utc().day;
  25. // => 9
  26. DateTime.utc()
  27. .set({ day: 4 })
  28. .toString();
  29. // => "2018-09-04T09:12:49.695Z"

Performance tests

Library Time
Moment 1349.161ms
Native 388.734ms
DateFns 602.132ms
DayJs 1088.253ms
Luxon 4728.460ms

Day of Week

Gets or sets the day of the week.

  1. // Moment.js
  2. moment().day();
  3. // => 0 (Sunday)
  4. moment().day(-14);
  5. // => "2018-08-26T09:12:49.695Z"
  6. // Native
  7. new Date().getDay();
  8. // => 0 (Sunday)
  9. new Date().setDate(new Date().getDate() - 14);
  10. // => "2018-08-26T09:12:49.695Z"
  11. // date-fns
  12. import getDay from 'date-fns/getDay';
  13. import setDay from 'date-fns/setDay';
  14. getDay(new Date());
  15. // => 0 (Sunday)
  16. setDay(new Date(), -14);
  17. // => "2018-08-26T09:12:49.695Z"
  18. // dayjs
  19. dayjs().day();
  20. // => 0 (Sunday)
  21. dayjs().set('day', -14);
  22. // => "2018-08-26T09:12:49.695Z"
  23. // Luxon
  24. DateTime.local().weekday;
  25. // => 7 (Sunday)
  26. DateTime.local()
  27. .minus({ day: 14 })
  28. .toJSDate();
  29. // => "2018-08-26T09:12:49.695Z"
Library Time
Moment 1869.609ms
Native 565.715ms
DateFns 815.355ms
DayJs 1087.407ms
Luxon 7093.800ms

Day of Year

Gets or sets the day of the year.

  1. // Moment.js
  2. moment().dayOfYear();
  3. // => 252
  4. moment().dayOfYear(256);
  5. // => "2018-09-13T09:12:49.695Z"
  6. // Native
  7. Math.floor(
  8. (new Date() - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24
  9. );
  10. // => 252
  11. // date-fns
  12. import getDayOfYear from 'date-fns/getDayOfYear';
  13. import setDayOfYear from 'date-fns/setDayOfYear';
  14. getDayOfYear(new Date());
  15. // => 252
  16. setDayOfYear(new Date(), 256);
  17. // => "2018-09-13T09:12:49.695Z"
  18. // dayjs ⚠️ requires dayOfYear plugin
  19. import dayOfYear from 'dayjs/plugin/dayOfYear'
  20. dayjs.extend(dayOfYear)
  21. dayjs().dayOfYear();
  22. // => 252
  23. dayjs().dayOfYear(256);
  24. // => "2018-09-13T09:12:49.695Z"
  25. // Luxon
  26. DateTime.local().ordinal;
  27. // => 252
  28. DateTime.local()
  29. .set({ ordinal: 256 })
  30. .toString();
  31. // => "2018-09-13T09:12:49.695Z"
Library Time
Moment 9851.425ms
Native 575.954ms
DateFns 1976.969ms
DayJs -
Luxon 5242.336ms

Week of Year

Gets or sets the week of the year.

  1. // Moment.js
  2. moment().week();
  3. // => 37
  4. moment().week(24);
  5. // => "2018-06-10T09:12:49.695Z"
  6. // date-fns
  7. import getWeek from 'date-fns/getWeek';
  8. import setWeek from 'date-fns/setWeek';
  9. getWeek(new Date());
  10. // => 37
  11. setWeek(new Date(), 24);
  12. // => "2018-06-10T09:12:49.695Z"
  13. // native getWeek
  14. const day = new Date();
  15. const MILLISECONDS_IN_WEEK = 604800000;
  16. const firstDayOfWeek = 1; // monday as the first day (0 = sunday)
  17. const startOfYear = new Date(day.getFullYear(), 0, 1);
  18. startOfYear.setDate(
  19. startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))
  20. );
  21. const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;
  22. // => 37
  23. // native setWeek
  24. const day = new Date();
  25. const week = 24;
  26. const MILLISECONDS_IN_WEEK = 604800000;
  27. const firstDayOfWeek = 1; // monday as the first day (0 = sunday)
  28. const startOfYear = new Date(day.getFullYear(), 0, 1);
  29. startOfYear.setDate(
  30. startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))
  31. );
  32. const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;
  33. day.setDate(day.getDate() - (dayWeek - week) * 7);
  34. day.toISOString();
  35. // => "2018-06-10T09:12:49.794Z
  36. // dayjs ⚠️ requires weekOfYear plugin
  37. import weekOfYear from 'dayjs/plugin/weekOfYear';
  38. dayjs.extend(weekOfYear);
  39. dayjs().week();
  40. // => 37
  41. dayjs().week(24);
  42. // => "2018-06-10T09:12:49.695Z"
  43. // Luxon
  44. DateTime.local().weekNumber;
  45. // => 37
  46. DateTime.local()
  47. .set({ weekNumber: 23 })
  48. .toString();
  49. // => "2018-06-10T09:12:49.794Z
Library Time
Moment 11997.187ms
Native 1467.277ms
DateFns 6070.445ms
DayJs -
Luxon 7944.387ms

Days in Month

Get the number of days in the current month.

  1. // Moment.js
  2. moment('2012-02', 'YYYY-MM').daysInMonth();
  3. // => 29
  4. // Native
  5. new Date(2012, 02, 0).getDate();
  6. // => 29
  7. // date-fns
  8. import getDaysInMonth from 'date-fns/getDaysInMonth';
  9. getDaysInMonth(new Date(2012, 1));
  10. // => 29
  11. // dayjs
  12. dayjs('2012-02').daysInMonth();
  13. // => 29
  14. // Luxon
  15. DateTime.local(2012, 2).daysInMonth;
  16. // => 29
Library Time
Moment 4836.243ms
Native 504.241ms
DateFns 1584.524ms
DayJs 2965.774ms
Luxon 1292.006ms

Weeks in Year

Gets the number of weeks in the current year, according to ISO weeks.

  1. // Moment.js
  2. moment().isoWeeksInYear();
  3. // => 52
  4. // date-fns
  5. import getISOWeeksInYear from 'date-fns/getISOWeeksInYear';
  6. getISOWeeksInYear(new Date());
  7. // => 52
  8. // dayjs ⚠️ requires isoWeeksInYear plugin
  9. import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear'
  10. dayjs.extend(isoWeeksInYear)
  11. dayjs().isoWeeksInYear();
  12. // => 52
  13. // Moment.js
  14. DateTime.local().weeksInWeekYear;
  15. // => 52
Library Time
Moment 983.551ms
Native -
DateFns 4751.236ms
DayJs -
Luxon 1743.877ms

Maximum of the given dates

Returns the maximum (most distant future) of the given date.

  1. const array = [
  2. new Date(2017, 4, 13),
  3. new Date(2018, 2, 12),
  4. new Date(2016, 0, 10),
  5. new Date(2016, 0, 9),
  6. ];
  7. // Moment.js
  8. moment.max(array.map(a => moment(a)));
  9. // => "2018-03-11T13:00:00.000Z"
  10. // Native
  11. new Date(Math.max.apply(null, array)).toISOString();
  12. // => "2018-03-11T13:00:00.000Z"
  13. // date-fns
  14. import max from 'date-fns/max';
  15. max(array);
  16. // => "2018-03-11T13:00:00.000Z"
  17. // dayjs ⚠️ requires minMax plugin
  18. import minMax from 'dayjs/plugin/minMax'
  19. dayjs.extend(minMax)
  20. dayjs.max(array.map(a => dayjs(a)));
  21. // => "2018-03-11T13:00:00.000Z"
  22. // Luxon
  23. DateTime.max(...array.map(a => DateTime.fromJSDate(a))).toJSDate();
  24. // => "2018-03-11T13:00:00.000Z"
Library Time
Moment 1736.095ms
Native 1104.626ms
DateFns 966.238ms
DayJs -
Luxon 2703.421ms

Minimum of the given dates

Returns the minimum (most distant future) of the given date.

  1. const array = [
  2. new Date(2017, 4, 13),
  3. new Date(2018, 2, 12),
  4. new Date(2016, 0, 10),
  5. new Date(2016, 0, 9),
  6. ];
  7. // Moment.js
  8. moment.min(array.map(a => moment(a)));
  9. // => "2016-01-08T13:00:00.000Z"
  10. // Native
  11. new Date(Math.min.apply(null, array)).toISOString();
  12. // => "2016-01-08T13:00:00.000Z"
  13. // date-fns
  14. import min from 'date-fns/min';
  15. min(array);
  16. // => "2016-01-08T13:00:00.000Z"
  17. // dayjs ⚠️ requires minMax plugin
  18. import minMax from 'dayjs/plugin/minMax'
  19. dayjs.extend(minMax)
  20. dayjs.min(array.map(a => dayjs(a)));
  21. // => "2016-01-08T13:00:00.000Z"
  22. // Luxon
  23. DateTime.min(...array.map(a => DateTime.fromJSDate(a))).toJSDate();
  24. // => "2016-01-08T13:00:00.000Z"
Library Time
Moment 1937.293ms
Native 1164.853ms
DateFns 908.990ms
DayJs -
Luxon 3085.444ms