Manipulate
Add
Add the specified number of days to the given date.
// Moment.js
moment().add(7, 'days');
// => "2018-09-16T09:12:49.695Z"
// Native
const now = new Date();
now.setDate(now.getDate() + 7);
// => "Sun Sep 16 2018 09:12:49"
// date-fns
import addDays from 'date-fns/addDays';
addDays(new Date(), 7);
// => "2018-09-16T09:12:49.695Z"
// dayjs
dayjs().add(7, 'day');
// => "2018-09-16T09:12:49.695Z"
// Luxon
DateTime.local()
.plus({ day: 7 })
.toJSDate();
// => "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.
// Moment.js
moment().subtract(7, 'days');
// => "2018-09-02T09:12:49.695Z"
// Native
new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7);
// => Sun Sep 09 2018 09:12:49
// date-fns
import subDays from 'date-fns/subDays';
subDays(new Date(), 7);
// => "2018-09-02T09:12:49.695Z"
// dayjs
dayjs().subtract(7, 'day');
// => "2018-09-02T09:12:49.695Z"
// Luxon
DateTime.local()
.minus({ day: 7 })
.toJSDate();
// => "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.
// 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"
// dayjs
dayjs().startOf('month');
// => "2018-08-31T14:00:00.000Z"
// Luxon
DateTime.local().startOf('month');
// => "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.
// Moment.js
moment().endOf('day');
// => "2018-09-09T13:59:59.999Z"
// Native
const end = new Date();
end.setHours(23, 59, 59, 999);
end.toISOString();
// => "2018-09-09T16:59:59.999Z"
// date-fns
import endOfDay from 'date-fns/endOfDay';
endOfDay(new Date());
// => "2018-09-09T13:59:59.999Z"
// dayjs
dayjs().endOf('day');
// => "2018-09-09T13:59:59.999Z"
// Luxon
DateTime.local().endOf('day');
// => "2018-09-02T09:12:49.695Z"
Library | Time |
---|---|
Moment | 4583.067ms |
Native | 284.882ms |
DateFns | 386.746ms |
DayJs | 1138.415ms |
Luxon | 19305.183ms |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .