Moment.js, A Simple Tool For Calculating Date And Time(feat.Nodejs)

Easy way to calculate date and time way better

Sometimes we need to calculate date and time.

Actually more than sometimes.

And this library, moment.js, is so powerful for your purpose.

Getting date, setting a date format whatsoever you want, subtracting date from date, counting days, and others.

Below listed some functions for your quick use.

Let’s dig it.


Install moment.js

Install library with below command.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install moment
npm install moment
npm install moment

Import the library with ‘require’ function.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var moment = require('moment');
moment();
//ES6 syntax
import moment from 'moment';
moment();
var moment = require('moment'); moment(); //ES6 syntax import moment from 'moment'; moment();
var moment = require('moment');
moment();

//ES6 syntax
import moment from 'moment';
moment();

Let’s get started from parsing date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const date = moment(""); //2022-01-23T11:15:00+09:00
const date = moment(""); //2022-01-23T11:15:00+09:00
const date = moment(""); //2022-01-23T11:15:00+09:00

We can display date using format() method with below tokens.

tokenmeanexample
YY(YY) year ex) YYYY -> 2022, YY -> 22
MM(MM) monthex) MMMM -> January, MM -> 01
DD dateex) DD -> 25
dd(dd) dayex) dd -> Tu, dddd -> Tuesday
hhhour(12)ex) hh -> 01
HH hour(24)ex) HH -> 13
mm minuteex) mm -> 07
sssecondex) ss -> 03
a am, pmex) a -> pm
Do ordinalex) Do -> 25th
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//January Tu 2022, 01:07:21 pm
moment.format('MMMM dd YYYY, hh:mm:ss a');
//January Tuesday 2022, pm 01:17
moment.format('MMMM dddd YYYY, a hh:mm')
// 01/25/2022
moment.format('MM/DD/YYYY');
//January Tu 2022, 01:07:21 pm moment.format('MMMM dd YYYY, hh:mm:ss a'); //January Tuesday 2022, pm 01:17 moment.format('MMMM dddd YYYY, a hh:mm') // 01/25/2022 moment.format('MM/DD/YYYY');
//January Tu 2022, 01:07:21 pm
moment.format('MMMM dd YYYY, hh:mm:ss a'); 

//January Tuesday 2022, pm 01:17
moment.format('MMMM dddd YYYY, a hh:mm') 

// 01/25/2022
moment.format('MM/DD/YYYY'); 

isValid() method works well on it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
moment('2022-01-25','YYYY-MM-DD').isValid(); //true
moment('2022-02-30','YYYY-MM-DD').isValid(); //false
moment('My birth is 2022-1-25','YYYY-MM-DD').isValid(); //true
moment('2022-01-25','YYYY-MM-DD').isValid(); //true moment('2022-02-30','YYYY-MM-DD').isValid(); //false moment('My birth is 2022-1-25','YYYY-MM-DD').isValid(); //true
moment('2022-01-25','YYYY-MM-DD').isValid(); //true

moment('2022-02-30','YYYY-MM-DD').isValid(); //false

moment('My birth is 2022-1-25','YYYY-MM-DD').isValid(); //true

Also parsing every detail date is available by intuitive methods.

hour()get hour
minute()get minute
second()get second
millisecond()get millisecond
date()get date (1~31)
day()get day of week (0~6)
week()get week of year (1~53)
month()get month (0~11)
year()get year
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
moment(new Date).hour(); //13
moment().millisecond(); //331
moment().week(); //5
moment(new Date).hour(); //13 moment().millisecond(); //331 moment().week(); //5
moment(new Date).hour(); //13

moment().millisecond(); //331

moment().week(); //5

To get the difference in date, use diff() method.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const theDate = moment('2021-01-20', 'YYYY-MM-DD');
const myDate = moment('2022-01-25', 'YYYY-MM-DD');
myDate.diff(theDate, 'days'); // 5
myDate.diff(theDate, 'year'); // 1
myDate.diff(theDate, 'month'); // 12
const theDate = moment('2021-01-20', 'YYYY-MM-DD'); const myDate = moment('2022-01-25', 'YYYY-MM-DD'); myDate.diff(theDate, 'days'); // 5 myDate.diff(theDate, 'year'); // 1 myDate.diff(theDate, 'month'); // 12
const theDate = moment('2021-01-20', 'YYYY-MM-DD');
const myDate = moment('2022-01-25', 'YYYY-MM-DD');

myDate.diff(theDate, 'days');     // 5
myDate.diff(theDate, 'year');    // 1
myDate.diff(theDate, 'month');  // 12

We can use this method for counting expire date or calculate D-day.

And there is a bunch of methods this library thanks to the last long maintenance till today.

Please check official web for further infomation.

momentjs.com