Frequently used api of Moment.js for date handling in development

Clayton
3 min readJul 17, 2023

--

Photo by Gabriel Heinzer on Unsplash

Moment.js is a popular datetime handling library you will use frequently in your daily frontend development. Below is the summary of the most common api for you.

Installation

npm install moment

Get Current Time

moment().valueOf() // Get timestamp in milliseconds

moment().startOf('day') // Start of today 00:00:00

moment().startOf('isoWeek') // Start of week, Monday 00:00:00

moment().startOf('month') // Start of month, 1st day 00:00:00

moment().endOf('day') // End of today 23:59:59

moment().endOf('isoWeek') // End of week, Sunday 23:59:59

moment().endOf('month') // End of month, last day 23:59:59

moment().year() // Get current year

moment().month() // Get current month (0-11, 0 is January, 11 is December)

moment().date() // Get today's date

moment().day() // Get current weekday (0-6, 0 is Sunday, 6 is Saturday)

moment().daysInMonth() // Get total days in current month

moment().month(moment().month() - 1).startOf('month').valueOf() // Start of previous month 00:00:00

moment().month(moment().month() - 1).endOf('month').valueOf() // End of previous month 23:59:59

Add/Subtract Time

moment().add(4, 'years').format('YYYY-MM-DD') // 2024-03-24 

moment().add(4, 'months').format('YYYY-MM-DD') // 2020-07-24

moment().add(4, 'days').format('YYYY-MM-DD') // 2020-03-28

moment([2016]).add(4, 'years').format('YYYY') // 2020

moment().subtract(10, 'years').format('YYYY') // 2010

moment().startOf('hour').format('YYYY-MM-DD HH:mm:ss') // 2020-03-24 15:00:00

moment().endOf('hour').format('YYYY-MM-DD HH:mm:ss') // 2020-03-24 15:59:59

Format Dates

moment().format('YYYY-MM-DD') 

moment().format('HH:mm:ss') // 24 hour format

moment().format('hh:mm:ss a') // 12 hour format

moment().format('x') // Timestamp in milliseconds

moment().format('X') // Timestamp in seconds

moment().format() // Default ISO format

// Year
moment().format('YYYY')

// Quarter
moment().format('Q')

// Month
moment().format('MMMM')

// Date
moment().format('DD')

// Day of year
moment().format('DDD')

// Day of week
moment().format('dddd')

// Custom format
moment().format('YYYY-MM-DD HH:mm:ss')

moment().format('YYYY[test]YYYY') // Escaping

Convert to Native JS Dates

moment().toDate() 

new Date(moment())

Set Date Values

moment().year(2019)

moment().month(9) // 0-11, January is 0

moment().date(2)

moment().isoWeekday(1) // Set to Monday of this week

moment().add(1, 'years')

moment().subtract(1, 'years')

Compare Dates

let startDate = moment().subtract(1, 'weeks')

let endDate = moment()

endDate.diff(startDate) // Returns difference in milliseconds

endDate.diff(startDate, 'months')

endDate.diff(startDate, 'weeks')

startDate.diff(endDate, 'days') // Negative value

Convert Timestamp Strings

moment(1259186487000).format('YYYY-MM-DD HH:mm:ss') // From milliseconds

moment('2009-11-26 06:01:27').valueOf() // To milliseconds

moment('2009-11-26 06:01:27') // Implicit conversion

Relative Time

moment('20091126', 'YYYYMMDD').fromNow() // '10 years ago'

moment('20091126', 'YYYYMMDD').fromNow(true) // '10 years'

moment().startOf('day').fromNow() // '15 hours ago'

moment().endOf('hour').fromNow() // 'in 24 minutes'

Query Dates

moment([2009]).isLeapYear()

moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28')

moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28', 'year')

moment('2009-11-26').isAfter('2020-11-26', 'year')

moment('2016-11-26').isBetween('2009-11-26', '2020-11-26')

moment('2009-11', 'YYYY-MM').daysInMonth() // Days in November 2009

moment().weeksInYear() // Weeks in current year

Durations

moment.duration(1500) // Create duration of 1500 milliseconds

moment.duration(1500).milliseconds()

moment.duration(1500).seconds()

moment.duration(1500).asSeconds()

moment.duration(2, 'seconds')

moment.duration(2, 'years')

Calendar Time

moment([2009]).isLeapYear(); // false

moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28'); // true

moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28', 'year'); // false

moment('2009-11-26').isAfter('2020-11-26', 'year'); // false

moment('2016-11-26').isBetween('2009-11-26', '2020-11-26'); // true

moment('2009-11', 'YYYY-MM').daysInMonth(); // 30

moment().weeksInYear(); // 52

--

--