Javascript Date & Time Cheatsheet

Javascript Date & Time Cheatsheet

September 18, 2020
Cheatsheets
★ Star me on GitHub

Since Moment.js is not recommended by official, here is a cheatsheet may help you migrate from it.


Here are some popular libraries that manipulates Date and Time in javascript:

  • moment:
    • Github Stars: 44.9k
    • min.js: 58.9 kB (version: 2.28.0)
  • Day.js
    • Github Stars: 29.5k
    • min.js: 6 kB (version: 1.8.26)
  • date-fns
    • Github Stars: 23.6k
    • function-per-file style

Install

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

Date Time Format

Moment.jsDay.jsdate-fnsExample
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)
Full docsFull docsFull docs

Current Datetime

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

// Note: I use fns.format here for testing convenience under Node.js env
// The better way to use the format function is via:
import { format } from 'date-fns' // ESM
import format from 'date-fns/format' // ES 2015

Parse Datetime

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月

Compare

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

Add / Subtract

// 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

StartOf / EndOf

// 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 date

// 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