Get + Set
Millisecond / Second / Minute / Hour
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
// date-fns
import getSeconds from 'date-fns/getSeconds';
import getHours from 'date-fns/getHours';
getSeconds(new Date());
// => 49
getHours(new Date());
// => 19
// dayjs
dayjs().second();
// => 49
dayjs().hour();
// => 19
// Luxon
DateTime.local().second;
// => 49
DateTime.local().hour;
// => 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.
// 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"
// date-fns
import setSeconds from 'date-fns/setSeconds';
import setHours from 'date-fns/setHours';
setSeconds(new Date(), 30);
// => "2018-09-09T09:12:30.695Z"
setHours(new Date(), 13);
// => "2018-09-09T03:12:49.695Z"
// dayjs
dayjs().set('second', 30);
// => "2018-09-09T09:12:30.695Z"
dayjs().set('hour', 13);
// => "2018-09-09T03:12:49.695Z"
// luxon
DateTime.utc()
.set({ second: 30 })
.toJSDate();
// => "2018-09-09T09:12:30.695Z"
DateTime.utc()
.set({ hour: 13 })
.toJSDate();
// => "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.
// 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"
// date-fns
import getDate from 'date-fns/getDate';
import setDate from 'date-fns/setDate';
getDate(new Date());
// => 9
setDate(new Date(), 4);
// => "2018-09-04T09:12:49.695Z"
// dayjs
dayjs().date();
// => 9
dayjs().set('date', 4);
// => "2018-09-04T09:12:49.695Z"
// luxon
DateTime.utc().day;
// => 9
DateTime.utc()
.set({ day: 4 })
.toString();
// => "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.
// Moment.js
moment().day();
// => 0 (Sunday)
moment().day(-14);
// => "2018-08-26T09:12:49.695Z"
// Native
new Date().getDay();
// => 0 (Sunday)
new Date().setDate(new Date().getDate() - 14);
// => "2018-08-26T09:12:49.695Z"
// date-fns
import getDay from 'date-fns/getDay';
import setDay from 'date-fns/setDay';
getDay(new Date());
// => 0 (Sunday)
setDay(new Date(), -14);
// => "2018-08-26T09:12:49.695Z"
// dayjs
dayjs().day();
// => 0 (Sunday)
dayjs().set('day', -14);
// => "2018-08-26T09:12:49.695Z"
// Luxon
DateTime.local().weekday;
// => 7 (Sunday)
DateTime.local()
.minus({ day: 14 })
.toJSDate();
// => "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.
// Moment.js
moment().dayOfYear();
// => 252
moment().dayOfYear(256);
// => "2018-09-13T09:12:49.695Z"
// Native
Math.floor(
(new Date() - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24
);
// => 252
// date-fns
import getDayOfYear from 'date-fns/getDayOfYear';
import setDayOfYear from 'date-fns/setDayOfYear';
getDayOfYear(new Date());
// => 252
setDayOfYear(new Date(), 256);
// => "2018-09-13T09:12:49.695Z"
// dayjs ⚠️ requires dayOfYear plugin
import dayOfYear from 'dayjs/plugin/dayOfYear'
dayjs.extend(dayOfYear)
dayjs().dayOfYear();
// => 252
dayjs().dayOfYear(256);
// => "2018-09-13T09:12:49.695Z"
// Luxon
DateTime.local().ordinal;
// => 252
DateTime.local()
.set({ ordinal: 256 })
.toString();
// => "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.
// Moment.js
moment().week();
// => 37
moment().week(24);
// => "2018-06-10T09:12:49.695Z"
// date-fns
import getWeek from 'date-fns/getWeek';
import setWeek from 'date-fns/setWeek';
getWeek(new Date());
// => 37
setWeek(new Date(), 24);
// => "2018-06-10T09:12:49.695Z"
// native getWeek
const day = new Date();
const MILLISECONDS_IN_WEEK = 604800000;
const firstDayOfWeek = 1; // monday as the first day (0 = sunday)
const startOfYear = new Date(day.getFullYear(), 0, 1);
startOfYear.setDate(
startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))
);
const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;
// => 37
// native setWeek
const day = new Date();
const week = 24;
const MILLISECONDS_IN_WEEK = 604800000;
const firstDayOfWeek = 1; // monday as the first day (0 = sunday)
const startOfYear = new Date(day.getFullYear(), 0, 1);
startOfYear.setDate(
startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))
);
const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;
day.setDate(day.getDate() - (dayWeek - week) * 7);
day.toISOString();
// => "2018-06-10T09:12:49.794Z
// dayjs ⚠️ requires weekOfYear plugin
import weekOfYear from 'dayjs/plugin/weekOfYear';
dayjs.extend(weekOfYear);
dayjs().week();
// => 37
dayjs().week(24);
// => "2018-06-10T09:12:49.695Z"
// Luxon
DateTime.local().weekNumber;
// => 37
DateTime.local()
.set({ weekNumber: 23 })
.toString();
// => "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.
// Moment.js
moment('2012-02', 'YYYY-MM').daysInMonth();
// => 29
// Native
new Date(2012, 02, 0).getDate();
// => 29
// date-fns
import getDaysInMonth from 'date-fns/getDaysInMonth';
getDaysInMonth(new Date(2012, 1));
// => 29
// dayjs
dayjs('2012-02').daysInMonth();
// => 29
// Luxon
DateTime.local(2012, 2).daysInMonth;
// => 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.
// Moment.js
moment().isoWeeksInYear();
// => 52
// date-fns
import getISOWeeksInYear from 'date-fns/getISOWeeksInYear';
getISOWeeksInYear(new Date());
// => 52
// dayjs ⚠️ requires isoWeeksInYear plugin
import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear'
dayjs.extend(isoWeeksInYear)
dayjs().isoWeeksInYear();
// => 52
// Moment.js
DateTime.local().weeksInWeekYear;
// => 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.
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"
// Native
new Date(Math.max.apply(null, array)).toISOString();
// => "2018-03-11T13:00:00.000Z"
// date-fns
import max from 'date-fns/max';
max(array);
// => "2018-03-11T13:00:00.000Z"
// dayjs ⚠️ requires minMax plugin
import minMax from 'dayjs/plugin/minMax'
dayjs.extend(minMax)
dayjs.max(array.map(a => dayjs(a)));
// => "2018-03-11T13:00:00.000Z"
// Luxon
DateTime.max(...array.map(a => DateTime.fromJSDate(a))).toJSDate();
// => "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.
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"
// Native
new Date(Math.min.apply(null, array)).toISOString();
// => "2016-01-08T13:00:00.000Z"
// date-fns
import min from 'date-fns/min';
min(array);
// => "2016-01-08T13:00:00.000Z"
// dayjs ⚠️ requires minMax plugin
import minMax from 'dayjs/plugin/minMax'
dayjs.extend(minMax)
dayjs.min(array.map(a => dayjs(a)));
// => "2016-01-08T13:00:00.000Z"
// Luxon
DateTime.min(...array.map(a => DateTime.fromJSDate(a))).toJSDate();
// => "2016-01-08T13:00:00.000Z"
Library | Time |
---|---|
Moment | 1937.293ms |
Native | 1164.853ms |
DateFns | 908.990ms |
DayJs | - |
Luxon | 3085.444ms |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .