Hello All,
Today, I am going to representing front of you how to work with Moment.js library in Node.js.
What is Moment.js ?
Moment.js is a Javascript library for parsing, validating, manipulating, formatting and displaying dates and times.
Why use Moment.js ?
It gets hard to handle date and time while developing web applications. As a developer, getting date from server then converting, comparing and displaying date and time might get tricky in Node.js.
Where to use Moment.js ?
Moment was designed to work both in the browser and in Node.js.
How to install Moment.js ?
1 |
npm install moment --save |
How to use Moment.js in Node.js?
1 2 |
var moment = require('moment'); moment().format(); |
Features provided by Moment.js
1. Traditional way to create a new date and Parse it with Moment.js.
1 2 |
const d = new Date(); // Wed Sep 26 2018 17:42:43 GMT+0530 (India Standard Time) const momentDate = moment(d); |
2. Format date with Moment.js.
1 2 3 4 5 6 7 8 9 |
// Only Date, Month and Year const date = moment(d).format('DD/MM/YYYY'); // 26/09/2018 // Date with Time const date = moment(d).format('DD/MM/YYYY HH:MM:SS'); // 26/09/2018 00:00:00 //date formats moment().format('MMMM Do YYYY, h:mm:ss a'); // September 26th 2018, 5:21:21 pm moment().format('dddd'); // Wednesday moment().format("MMM Do YY"); // Sep 26th 18 moment().format(); // 2018-09-26T17:21:21+05:30 |
3. Converting string to date.
Moment.js provide a simple way to convert string to date.
1 2 3 4 |
const string = '26 Sep 2018'; const date = moment(string).format('DD/MM/YYYY'); // 26/09/2018 // OR const date = moment(string, 'DD/MM/YYYY') // 26/09/2018 |
4. Convert date to string.
Moment.js provide a simple way to convert date to string.
1 |
const date = moment().toString(); // Sun Sep 26 2018 00:00:00 |
5. Date validation.
Moment.js have predefined method to validate date. You can check whether the Moment considers the date invalid using moment().isValid
.
1 2 |
const date = moment('26 Sep 2018').isValid(); // true const date = moment('26 2018').isValid(); // false |
6. Add day,month and year
With the help of Moment.js library we can easily add day, month and year in date.
1 2 3 |
const day = moment().add(1, 'day').toString(); // wed Sep 27 2018 00:00:00 const month = moment().add(1, 'month').toString(); // Fri Oct 26 2018 00:00:00 const year = moment().add(1, 'year').toString(); // Thu Sep 26 2019 00:00:00 |
7. Subtract day,month and year.
With the help of Moment.js library we can easily subtract date by day, month and year.
1 2 3 |
const day = moment().subtract(1, 'day').toString(); // Wed Sep 25 2018 00:00:00 const month = moment().subtract(1, 'month').toString(); //Sun Aug 26 2018 00:00:00 const year = moment().subtract(1, 'year').toString(); // Thu Sep 26 2017 00:00:00 |
8. Difference between two dates
1 2 3 4 5 6 7 8 9 10 |
const x = moment('26 Sep 2018'); const y = moment('26 Oct 2019'); // By days y.diff(x, 'days'); // 396 // By months y.diff(x, 'month'); // 13 //By weeks y.diff(x,'weeks'); // 56 // By years y.diff(x, 'years'); // 1 |
I hope you get help full information from my blog.
reference: https://momentjs.com/
Please comments and share your experience with me to make us better next time.
Recent Comments