Display

Format

Return the formatted date string in the given format.

  1. // Moment.js
  2. moment().format('dddd, MMMM Do YYYY, h:mm:ss A');
  3. // => "Sunday, September 9th 2018, 7:12:49 PM"
  4. moment().format('ddd, hA');
  5. // => "Sun, 7PM"
  6. // date-fns
  7. import format from 'date-fns/format';
  8. format(new Date(), 'eeee, MMMM do YYYY, h:mm:ss aa');
  9. // => "Sunday, September 9th 2018, 7:12:49 PM"
  10. format(new Date(), 'eee, ha');
  11. // => "Sun, 7PM"
  12. // dayjs
  13. dayjs().format('dddd, MMMM D YYYY, h:mm:ss A');
  14. // => "Sunday, September 9 2018, 7:12:49 PM"
  15. dayjs().format('ddd, hA');
  16. // => "Sun, 7PM"
  17. // dayjs ⚠️ requires advancedFormat plugin to support more format tokens
  18. import advancedFormat from 'dayjs/plugin/customParseFormat'
  19. dayjs.extend(advancedFormat)
  20. dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A');
  21. // => "Sunday, September 9th 2018, 7:12:49 PM"
  22. // Luxon
  23. DateTime.fromMillis(time).toFormat('EEEE, MMMM dd yyyy, h:mm:ss a');
  24. // => "Sunday, September 9 2018, 7:12:49 PM" ⚠️ not support 9th
  25. DateTime.fromMillis(time).toFormat('EEE, ha');
  26. // => "Sun, 7PM"

Time from now

Return time from now.

  1. // Moment.js
  2. moment(1536484369695).fromNow();
  3. // => "4 days ago"
  4. // date-fns
  5. import formatDistance from 'date-fns/formatDistance';
  6. formatDistance(new Date(1536484369695), new Date(), { addSuffix: true });
  7. // => "4 days ago"
  8. // dayjs ⚠️ requires relativeTime plugin
  9. import relativeTime from 'dayjs/plugin/relativeTime';
  10. dayjs.extend(relativeTime);
  11. dayjs(1536484369695).fromNow();
  12. // => "5 days ago" ⚠️ the rounding method of this plugin is different from moment.js and date-fns, use with care.
  13. // luxon ❌ does not support relative time

Time from x

Return time from x.

  1. // Moment.js
  2. moment([2007, 0, 27]).to(moment([2007, 0, 29]));
  3. // => "in 2 days"
  4. // date-fns
  5. import formatDistance from 'date-fns/formatDistance';
  6. formatDistance(new Date(2007, 0, 27), new Date(2007, 0, 29));
  7. // => "2 days"
  8. // dayjs ⚠️ requires relativeTime plugin
  9. import relativeTime from 'dayjs/plugin/relativeTime';
  10. dayjs.extend(relativeTime);
  11. dayjs('2007-01-27').to(dayjs('2007-01-29'));
  12. // => "in 2 days"
  13. // luxon ❌ does not support relative time

Difference

Get the unit of time between the given dates.

  1. // Moment.js
  2. moment([2007, 0, 27]).diff(moment([2007, 0, 29]));
  3. // => -172800000
  4. moment([2007, 0, 27]).diff(moment([2007, 0, 29]), 'days');
  5. // => -2
  6. // Native
  7. new Date(2007, 0, 27) - new Date(2007, 0, 29);
  8. // => -172800000
  9. Math.ceil(
  10. (new Date(2007, 0, 27) - new Date(2007, 0, 29)) / 1000 / 60 / 60 / 24
  11. );
  12. // => -2
  13. // date-fns
  14. import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
  15. differenceInMilliseconds(new Date(2007, 0, 27), new Date(2007, 0, 29));
  16. // => -172800000
  17. import differenceInDays from 'date-fns/differenceInDays';
  18. differenceInDays(new Date(2007, 0, 27), new Date(2007, 0, 29));
  19. // => -2
  20. // dayjs
  21. dayjs('2007-01-27').diff(dayjs('2007-01-29'), 'milliseconds');
  22. // => -172800000
  23. dayjs('2007-01-27').diff(dayjs('2007-01-29'), 'days');
  24. // => -2
  25. // luxon
  26. DateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29)).milliseconds;
  27. // => -172800000
  28. DateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29), 'days').days;
  29. // => -2