Moment.js is a fantastic time & date library.
date-fns is a fantastic time & date library & tiny.
You-Dont-Need-Momentjs is a fantastic recommendation & ad of date-fns.
Problems with date-fns:
- Still need to download or clone from npm.
- Still need to import "date-fns" as a closure(or namespace/module/package/component/class/no matter what) in each business logic file.
Only using native Date static class to be a pure replacement in any other third packages, including date-fns.
Removed moment.js
- npm uninstall -g moment
- yarn remove moment
Removed date-fns
- npm uninstall -g date-dns
- yarn remove date-dns
- Millisecond/Second/Minute/Hour
- Date of Month
- Day of Week
- Day of Year
- Week of Year
- Days in Month
- Weeks in Year
- Maximum of the given dates
- Minimum of the given dates
Return the date parsed from date string using the given format string.
// Moment.js
moment("12-25-1995", "MM-DD-YYYY");
// => "1995-12-24T13:00:00.000Z"
// date-fns
import parse from "date-fns/parse";
parse("12-25-1995", "MM-dd-yyyy", new Date());
// => "1995-12-24T13:00:00.000Z"
/* native */
new Date('12-25-1995').toISOString()
// => "1995-12-24T16:00:00.000Z"
Return the date parsed from time string using the given format string.
// Moment.js
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm");
// => "2010-10-19T17:30:00.000Z"
// date-fns
import parse from "date-fns/parse";
parse("2010-10-20 4:30", "yyyy-MM-dd H:mm", new Date());
// => "2010-10-19T17:30:00.000Z"
/* native */
new Date("2010-10-20 01:30").toISOString('zh-CN')
// => "2010-10-19T17:30:00.000Z"
Return the date parsed from string using the given format string and locale.
// Moment.js
moment("2012 mars", "YYYY MMM", "fr");
// => "2012-02-29T13:00:00.000Z"
// date-fns
import parse from "date-fns/parse";
import fr from "date-fns/locale/fr";
parse("2012 mars", "yyyy MMMM", new Date(), { locale: fr });
// => "2012-02-29T13:00:00.000Z"
/* native */
new Date("2012 mars").toISOString({ locale: 'fr' })
Get the Millisecond/Second/Minute/Hour
of the given date.
// Moment.js
moment().seconds();
// => 49
moment().hours();
// => 19
// Native
new Date().getSeconds();
// => 49
new Date().getHours();
// => 19
/* another native */
Date.now() % 1000 % 60
// => 49
Date.now() % 1000 % 60 % 24
// => 19
Set the Millisecond/Second/Minute/Hour
of the given date.
// Moment.js
moment().seconds(30);
// => "2018-09-09T09:12:30.695Z"
moment().hours(13);
// => "2018-09-09T03:12:49.695Z"
// Native
new Date(new Date().setSeconds(30));
// => "2018-09-09T09:12:30.695Z"
new Date(new Date().setHours(13));
// => "2018-09-09T03:12:49.695Z"
/* native local */
new Date(new Date().setSeconds(30)).toISOString()
// => "2018-09-09T09:12:30.695Z"
new Date(new Date().setHours(13)).toISOString()
// => "2018-09-09T03:12:49.695Z"
Gets or sets the day of the month.
// Moment.js
moment().date();
// => 9
moment().date(4);
// => "2018-09-04T09:12:49.695Z"
// Native
new Date().getDate();
// => 9
new Date().setDate(4);
// => "2018-09-04T09:12:49.695Z"
/* native */
new Date(new Date().setDate(4)).toISOString()
// => "2018-09-04T14:07:37.420Z"
Gets or sets the day of the week.
// Moment.js
moment().day();
// => 0
moment().day(-14);
// => "2018-08-26T09:12:49.695Z"
// Native
new Date().getDay();
// => 0
new Date().setDate(new Date().getDate() - 14);
// => "2018-08-26T09:12:49.695Z"
/* ament it at locally */
new Date(new Date().setDate(new Date().getDate() - 14)).toISOString()
// => "2018-08-26T09:12:49.695Z"
/* another Native */
new Date(Date.now() - 1000*60*60*24*14).toISOString()
// => "2018-08-26T09:12:49.695Z"
Gets or sets the day of the year.
// Moment.js
moment().dayOfYear();
// => 252
moment().dayOfYear(256);
// => "2018-09-13T09:12:49.695Z"
// date-fns
import getDayOfYear from "date-fns/getDayOfYear";
getDayOfYear(new Date());
// => 252
import setDayOfYear from "date-fns/setDayOfYear";
setDayOfYear(new Date(), 256);
// => "2018-09-13T09:12:49.695Z"
/* native */
/* +new Date(new Date().getFullYear()+'') === Date.UTC(new Date().getFullYear()) */
(Date.now()-+new Date(new Date().getFullYear()+''))/1000/60/60/24|0
// => 256
//// setDayOfYear
new Date(256 *24*60*60*1000 + +new Date(new Date().getFullYear().toString())).toISOString()
// "2018-09-14T00:00:00.000Z"
Gets or sets the week of the year.
// Moment.js
moment().week();
// => 37
moment().week(24);
// => "2018-06-10T09:12:49.695Z"
// date-fns
import getWeek from "date-fns/getWeek";
getWeek(new Date());
// => 37
import setWeek from "date-fns/setWeek";
setWeek(new Date(), 24);
// => "2018-06-10T09:12:49.695Z"
/* native */
Math.ceil((Date.now()-+new Date(new Date().getFullYear()+''))/1000/60/60/24/7)
// 37
Get the number of days in the current month.
// Moment.js
moment("2012-02", "YYYY-MM").daysInMonth();
// => 29
// date-fns
import getDaysInMonth from "date-fns/getDaysInMonth";
getDaysInMonth(new Date(2012, 1));
// => 29
/* native */
(+new Date('2012/3')-+new Date('2012/2'))/1000/60/60/24
// => 29
Gets the number of weeks in the current year, according to ISO weeks.
// Moment.js
moment().isoWeeksInYear();
// => 52
// date-fns
import getISOWeeksInYear from "date-fns/getISOWeeksInYear";
getISOWeeksInYear(new Date());
// => 52
/* native */
(+new Date('2019')-+new Date('2018'))/1000/60/60/24/7>>0
Returns the maximum (most distant future) of the given date.
const array = [
new Date(2017, 4, 13),
new Date(2018, 2, 12),
new Date(2016, 0, 10),
new Date(2016, 0, 9)
];
// Moment.js
moment.max(array.map(a => moment(a)));
// => "2018-03-11T13:00:00.000Z"
// date-fns
import max from "date-fns/max";
max(array);
// => "2018-03-11T13:00:00.000Z"
/* native es6 */
new Date(Math.max(...array.map(m => +m))).toISOString()
/* native es5 */
/*
* es5- polyfill(or sort):
* Array.prototype.map = function(arr){
* //do sth map/reduce polyfilled.
* return arr
* }
new Date(Math.max.apply(Math, array.map(function(m){ return +m }))).toISOString()
Returns the minimum (most distant future) of the given date.
const array = [
new Date(2017, 4, 13),
new Date(2018, 2, 12),
new Date(2016, 0, 10),
new Date(2016, 0, 9)
];
// Moment.js
moment.min(array.map(a => moment(a)));
// => "2016-01-08T13:00:00.000Z"
// date-fns
import min from "date-fns/min";
min(array);
// => "2016-01-08T13:00:00.000Z"
/* native es6 */
new Date(Math.min(...array.map(m => +m))).toISOString()
/* native es5 */
new Date(Math.min.apply(Math, array.map(function(m){ return +m }))).toISOString()
Add the specified number of days to the given date.
// Moment.js
moment().add(7, "days");
// => "2018-09-16T09:12:49.695Z"
// date-fns
import addDays from "date-fns/addDays";
addDays(new Date(), 7);
// => "2018-09-16T09:12:49.695Z"
/* native */
new Date(Date.now() + 1000*60*60*24*7).toISOString()
// => "2018-09-20T10:35:26.795Z"
Subtract the specified number of days from the given date.
// Moment.js
moment().subtract(7, "days");
// => "2018-09-02T09:12:49.695Z"
// date-fns
import subDays from "date-fns/subDays";
subDays(new Date(), 7);
// => "2018-09-02T09:12:49.695Z"
/* native */
new Date(Date.now() - 1000*60*60*24*7).toISOString()
// => "2018-09-02T09:12:49.695Z"
Return the start of a unit of time for the given date.
// Moment.js
moment().startOf("month");
// => "2018-08-31T14:00:00.000Z"
// date-fns
import startOfMonth from "date-fns/startOfMonth";
startOfMonth(new Date());
// => "2018-08-31T14:00:00.000Z"
/* native */
new Date(Date.UTC( 2018, new Date().getMonth())-8 *60*60*1000).toISOString()
// => "2018-08-31T16:00:00.000Z"
Return the end of a unit of time for the given date.
// Moment.js
moment().endOf("day");
// => "2018-09-09T13:59:59.999Z"
// date-fns
import endOfDay from "date-fns/endOfDay";
endOfDay(new Date());
// => "2018-09-09T13:59:59.999Z"
/* native */
//`${new Date().getFullYear()}/${new Date().getMonth()+1}/${new Date().getDate()}` === '2018/9/15'
new Date(+new Date('2018/9/15')+24*60*60*1000 - 1000)
// => "Sat Sep 15 2018 23:59:59 GMT+0800 (中国标准时间)"
Return the formatted date string in the given format.
// Moment.js
moment().format("dddd, MMMM Do YYYY, h:mm:ss A");
// => "Sunday, September 9th 2018, 7:12:49 PM"
moment().format("ddd, hA");
// => "Sun, 7PM"
// date-fns
import format from "date-fns/format";
format(new Date(), "eeee, MMMM do YYYY, h:mm:ss aa");
// => "Sunday, September 9th 2018, 7:12:49 PM"
format(new Date(), "eee, ha");
// => "Sun, 7PM"
/* native */
//new Date().toLocaleDateString('en', { weekday: 'long' }).slice(0,3)
new Date().toString().slice(0,3) + ',' + new Date().toLocaleString('en').replace(/[^AM|PM]/img,'')
// => "Sat,AM"
Return time from now.
// Moment.js
moment([2018, 8, 9]).fromNow();
// => "about 4 hours ago"
// date-fns
import formatDistance from "date-fns/formatDistance";
formatDistance(new Date(2018, 8, 9), new Date(), { addSuffix: true });
// => "4 hours ago"
/* native */
Math.round((Date.now()-+new Date('2018/9/14'))/1000/60/60)
// => 24
// new Date().getMinutes() => 30
// => 24 hours 30 minutes ago
Return time from x.
// Moment.js
moment([2007, 0, 27]).to(moment([2007, 0, 29]));
// => "in 2 days"
// date-fns
import formatDistance from "date-fns/formatDistance";
formatDistance(new Date(2007, 0, 27), new Date(2007, 0, 29));
// => "2 days"
/* native */
Math.abs((+new Date(2007,0,27) - +new Date(2007,0,29))/1000/60/60/24)
// => 2
Get the unit of time between the given dates.
// Moment.js
moment([2007, 0, 27]).diff(moment([2007, 0, 29]));
// => -172800000
moment([2007, 0, 27]).diff(moment([2007, 0, 29]), "days");
// => -2
// date-fns
import differenceInMilliseconds from "date-fns/differenceInMilliseconds";
differenceInMilliseconds(new Date(2007, 0, 27), new Date(2007, 0, 29));
// => -172800000
import differenceInDays from "date-fns/differenceInDays";
differenceInDays(new Date(2007, 0, 27), new Date(2007, 0, 29));
// => -2
/* native */
Date.UTC(2007,0,27) - Date.UTC(2007,0,29)
// => -172800000 -_-
Check if a date is before another date.
// Moment.js
moment("2010-10-20").isBefore("2010-10-21");
// => true
// date-fns
import isBefore from "date-fns/isBefore";
isBefore(new Date(2010, 9, 20), new Date(2010, 9, 21));
// => true
/* native */
Date.UTC(2010,9,20) - Date.UTC(2010,9,21) < 0
// isBefore? => ture -_-
Check if a date is same another date.
// Moment.js
moment("2010-10-20").isSame("2010-10-21");
// => false
moment("2010-10-20").isSame("2010-10-20");
// => true
// date-fns
import isSameDay from "date-fns/isSameDay";
isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 21));
// => false
isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 20));
// => true
/* native */
Date.UTC(2010,9,20) - Date.UTC(2010,9,20) === 0
// isSame? => ture -_-
Check if a date is after another date.
// Moment.js
moment("2010-10-20").isAfter("2010-10-19");
// => true
// date-fns
import isAfter from "date-fns/isAfter";
isAfter(new Date(2010, 9, 20), new Date(2010, 9, 19));
// => true
/* native */
// => -_-
Check if a date is between two other dates.
// Moment.js
moment("2010-10-20").isBetween("2010-10-19", "2010-10-25");
// => true
// date-fns
import isWithinInterval from "date-fns/isWithinInterval";
isWithinInterval(new Date(2010, 9, 20), {
start: new Date(2010, 9, 19),
end: new Date(2010, 9, 25)
});
// => true
Check if a year is a leap year.
// Moment.js
moment([2000]).isLeapYear();
// => true
// date-fns
import isLeapYear from "date-fns/isLeapYear";
isLeapYear(new Date(2000, 0, 1));
// => true
/* native */
Date.UTC(2001) - Date.UTC(2000) === 31622400000
// => true
Check if a variable is a native js Date object.
// Moment.js
moment.isDate(new Date());
// => true
// date-fns
import isDate from "date-fns/isDate";
isDate(new Date());
// => true
/* native */
new Date(new Date()).toString() !== 'Invalid Date'
// => true
MIT