Manipulate

Add

Add the specified number of days to the given date.

  1. // Moment.js
  2. moment().add(7, 'days');
  3. // => "2018-09-16T09:12:49.695Z"
  4. // Native
  5. const now = new Date();
  6. now.setDate(now.getDate() + 7);
  7. // => "Sun Sep 16 2018 09:12:49"
  8. // date-fns
  9. import addDays from 'date-fns/addDays';
  10. addDays(new Date(), 7);
  11. // => "2018-09-16T09:12:49.695Z"
  12. // dayjs
  13. dayjs().add(7, 'day');
  14. // => "2018-09-16T09:12:49.695Z"
  15. // Luxon
  16. DateTime.local()
  17. .plus({ day: 7 })
  18. .toJSDate();
  19. // => "2018-09-16T09:12:49.695Z"
Library Time
Moment 1468.151ms
Native 208.735ms
DateFns 337.129ms
DayJs 631.982ms
Luxon 7248.459ms

Subtract

Subtract the specified number of days from the given date.

  1. // Moment.js
  2. moment().subtract(7, 'days');
  3. // => "2018-09-02T09:12:49.695Z"
  4. // Native
  5. new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7);
  6. // => Sun Sep 09 2018 09:12:49
  7. // date-fns
  8. import subDays from 'date-fns/subDays';
  9. subDays(new Date(), 7);
  10. // => "2018-09-02T09:12:49.695Z"
  11. // dayjs
  12. dayjs().subtract(7, 'day');
  13. // => "2018-09-02T09:12:49.695Z"
  14. // Luxon
  15. DateTime.local()
  16. .minus({ day: 7 })
  17. .toJSDate();
  18. // => "2018-09-02T09:12:49.695Z"
Library Time
Moment 1638.627ms
Native 246.940ms
DateFns 759.963ms
DayJs 954.443ms
Luxon 7701.059ms

Start of Time

Return the start of a unit of time for the given date.

  1. // Moment.js
  2. moment().startOf('month');
  3. // => "2018-08-31T14:00:00.000Z"
  4. // date-fns
  5. import startOfMonth from 'date-fns/startOfMonth';
  6. startOfMonth(new Date());
  7. // => "2018-08-31T14:00:00.000Z"
  8. // dayjs
  9. dayjs().startOf('month');
  10. // => "2018-08-31T14:00:00.000Z"
  11. // Luxon
  12. DateTime.local().startOf('month');
  13. // => "2018-09-02T09:12:49.695Z"
Library Time
Moment 1869.290ms
Native -
DateFns 455.759ms
DayJs 735.666ms
Luxon 5116.801ms

End of Time

Return the end of a unit of time for the given date.

  1. // Moment.js
  2. moment().endOf('day');
  3. // => "2018-09-09T13:59:59.999Z"
  4. // Native
  5. const end = new Date();
  6. end.setHours(23, 59, 59, 999);
  7. end.toISOString();
  8. // => "2018-09-09T16:59:59.999Z"
  9. // date-fns
  10. import endOfDay from 'date-fns/endOfDay';
  11. endOfDay(new Date());
  12. // => "2018-09-09T13:59:59.999Z"
  13. // dayjs
  14. dayjs().endOf('day');
  15. // => "2018-09-09T13:59:59.999Z"
  16. // Luxon
  17. DateTime.local().endOf('day');
  18. // => "2018-09-02T09:12:49.695Z"
Library Time
Moment 4583.067ms
Native 284.882ms
DateFns 386.746ms
DayJs 1138.415ms
Luxon 19305.183ms