Javascript 日期时间速查表

Javascript 日期时间速查表

September 18, 2020
速查表
★ 在 GitHub 赞我

现在官方也不再推荐使用 Moment.js,因此我制作了一个速查表,帮助你从 Moment.js 中迁移出来。


以下是一些 Javascript 流行的日期/时间处理库:

  • Moment.js:
    • Github 星数: 44.9k
    • min.js: 58.9 kB (版本: 2.28.0)
  • Day.js
    • Github 星数: 29.5k
    • min.js: 6 kB (版本: 1.8.26)
  • date-fns
    • Github 星数: 23.6k
    • 一个函数一个文件的风格

安装

npm install moment --save
npm install dayjs --save
npm install date-fns --save

日期时间格式

Moment.jsDay.jsdate-fns示例
MM/DD/YYYY HH:mm:ssMM/DD/YYYY HH:mm:ssMM/dd/yyyy HH:mm:ss09/17/2020 23:45:02
ZZXXX+08:00 (Timezone)
ddddddcccThu (Weekday)
ddddddddiiiiTuesday (Weekday)
MMMMMMMMMSep (Month)
DDd1..31 (Month Day)
SSSSSSSSS000..999
(Milliseconds)
Xunix()t1600357502
(Unix timestamp)
xvalueOf()T1600357502945
(Millisecond Unix timestamp)
完整文档完整文档完整文档

当前日期时间

import moment from 'moment'
let formatStr = "MM/DD/YYYY HH:mm:ss"
let current = moment().format(formatStr)
console.log(current)
// 09/17/2020 23:45:02
import dayjs from "dayjs"
let formatStr = "MM/DD/YYYY HH:mm:ss"
let current = dayjs().format(formatStr)
console.log(current)
// 09/17/2020 23:45:02
import fns from 'date-fns'
let formatStr = "MM/dd/yyyy HH:mm:ss"
let current = fns.format(new Date(), formatStr)
console.log(current)
// 09/17/2020 23:45:02

// 注意: 我使用 fns.format 的方式,是为了方便在 Node.js 环境下测试代码
// 更推荐以下的方式去引用 fns 的函数:
import { format } from 'date-fns' // ESM
import format from 'date-fns/format' // ES 2015

解析日期时间

let formatStr = "MM/DD/YYYY HH:mm:ss"
let dateStr = "09/17/2020 23:45:02"
let current = moment(dateStr, formatStr)
console.log(current)
// Moment<2020-09-17T23:45:02+08:00>
let formatStr = "MM/DD/YYYY HH:mm:ss"
let dateStr = "09/17/2020 23:45:02"
let current = dayjs(dateStr, formatStr)
console.log(current)
// d {
//   '$L': 'en',
//   '$d': 2020-09-17T15:45:02.000Z,
//   '$y': 2020,
//   '$M': 8,
//   '$D': 17,
//   '$W': 4,
//   '$H': 23,
//   '$m': 45,
//   '$s': 2,
//   '$ms': 0
// }
let formatStr = "MM/dd/yyyy HH:mm:ss"
let dateStr = "09/17/2020 23:45:02"
let current = fns.parse(dateStr, formatStr, new Date())
console.log(current)
// 2020-09-17T15:45:02.000Z (native Date object)

I18n 国际化

moment.locale("zh-cn")
let current = moment().format("MMM")
console.log(current)
// 9月
import 'dayjs/locale/zh-cn.js' // load on demand
dayjs.locale('zh-cn') // locale globally
dayjs(new Date()).locale('zh-cn').format("MMM") // locale in a specific instance
// 9月
import zhCN from 'date-fns/locale/zh-CN/index.js'
let current = fns.format(new Date(), "MMM", {locale: zhCN})
console.log(current)
// 9月

比较

let oldDate = moment("09/17/2020", "MM/DD/YYYY")
let current = moment()
console.log(oldDate < current)
// true
let oldDate = dayjs("09/17/2020", "MM/DD/YYYY")
let current = dayjs()
console.log(oldDate < current)
// true
let oldDate = fns.parse("09/17/2020", "MM/dd/yyyy", new Date())
console.log(fns.isBefore(oldDate, new Date()))
// true

console.log(fns.compareAsc(oldDate, new Date()))
// -1
// Doc: https://date-fns.org/v2.16.1/docs/compareAsc

加 / 减

// seconds, minutes, hours, days, weeks, months, years
let tormorow = moment().add(1, "days")
console.log(tormorow)
// Moment<2020-09-19T17:33:36+08:00>

let lastWeek = moment().subtract(1, "weeks")
console.log(lastWeek)
// Moment<2020-09-11T17:35:10+08:00>

let nextMonth = moment().add(1, "months")
console.log(nextMonth)
// Moment<2020-10-18T17:35:40+08:00>
// seconds, minutes, hours, days, weeks, months, years
let tormorow = dayjs().add(1, "days")
console.log(tormorow)
// d {
//  '$d': 2020-09-19T09:43:25.683Z,
//  ...
// }

let lastWeek = dayjs().subtract(1, "weeks")
console.log(lastWeek)
// d {
//  '$d': 2020-09-11T09:43:25.684Z,
//  ...
// }

let nextMonth = dayjs().add(1, "months")
console.log(nextMonth)
// d {
//  '$d': 2020-10-18T09:43:25.684Z,
//  ...
// }
let tormorow = fns.add(new Date(), {days: 1})
console.log(tormorow)
// 2020-09-19T09:47:52.834Z

let lastWeek = fns.add(new Date(), {weeks: -1})
console.log(lastWeek)
// 2020-09-11T09:47:52.834Z

let nextMonth = fns.add(new Date(), {months: 1})
console.log(nextMonth)
// 2020-10-18T09:47:52.834Z

开始 / 结束

// second, minute, hour, day, week, month, year
let start = moment().startOf("day")
console.log(start)
// Moment<2020-09-18T00:00:00+08:00>

let end = moment().endOf("month")
console.log(end)
// Moment<2020-09-30T23:59:59+08:00>
// second, minute, hour, day, week, month, year
let start = dayjs().startOf("day")
console.log(start)
// d {
//  '$d': 2020-09-17T16:00:00.000Z,
//}

let end = dayjs().endOf("month")
console.log(end)
// d {
//  '$d': 2020-09-30T15:59:59.999Z,
//}
// startOf[Second, Minute, Hour, Day, Week, Month, Year],
// endOf[Second, Minute, Hour, Day, Week, Month, Year] 
let start = fns.startOfDay(new Date())
console.log(start)
// 2020-09-17T16:00:00.000Z

let end = fns.endOfMonth(new Date())
console.log(end)
// 2020-09-30T15:59:59.999Z

对比日期时间(diff)

// seconds, minutes, hours, days, weeks, months, years
let date1 = moment("01/01/2018", "MM/DD/YYYY")
let date2 = moment()
console.log(date1.diff(date2, 'days'))
// -991
console.log(date1.diff(date2, 'years'))
// -2
// seconds, minutes, hours, days, weeks, months, years
let date1 = dayjs("01/01/2018", "MM/DD/YYYY")
let date2 = dayjs()
console.log(date1.diff(date2, 'days'))
// -991
console.log(date1.diff(date2, 'years'))
// -2
// differenceIn[Seconds, Minutes, Hours, Days, Weeks, Months, Years]
let date1 = fns.parse('01/01/2018', 'MM/dd/yyyy', new Date())
let output = fns.differenceInDays(date1, new Date())
console.log(output)
// -991

let output = fns.differenceInYears(date1, new Date())
// -1