Working with Moment.js

Source: https://medium.com/@hnmtechshack/working-with-moment-js-8393b6dccfcb

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 on front-end side. Don’t worry, we got a library for you.
That’s Moment.js
Moment.js is a Javascript library for parsing, validating, manipulating, formatting and displaying dates and times.

Installing Moment.js

  1. Content Delivery Network (CDN)
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js
2. Bower
$ bower install moment --save
3. NPM
$ npm install moment --save
Let’s get started, I will be using ES2015 (ES6) Javascript for code.

A traditional way to create a new date

const d = new Date(); // Sun Jan 01 2017 00:00:00

Parse it with Moment

const momentDate = moment(d);

Format date

// Only Date, Month and Year
const date = moment(d).format('DD/MM/YYYY'); // 01/01/2017
// Date with Time
const date = moment(d).format('DD/MM/YYYY HH:MM:SS'); // 01/01/2017 00:00:00

Convert string to date

const string = '01 Mar 2017';
const date = moment(string).format('DD/MM/YYYY'); // 01/03/2017
// OR
const date = moment(string, 'DD/MM/YYYY') // 01/03/2017

Convert date to string

const date = moment().toString(); // Sun Jan 01 2017 00:00:00

Get relevant time from current time

const d = moment('01 Jan 2017');
const date = moment(d).fromNow(); // 2 months ago

Check whether date is valid

const date = moment('01 Jan 2017').isValid(); // true
const date = moment('01 2017').isValid(); // false

Convert to epoch time

const date = moment('01 Jan 2017').unix(); // 1483209000

Convert to UTC

const date = moment('01 Jan 2017').utc().format();

Convert to local time

const date = moment('01 Jan 2017').utc().format();

Add day, month or year

const day = moment().add(1, 'day').toString(); // Mon Jan 02 2017 00:00:00
const month = moment().add(1, 'month').toString(); // Wed Feb 01 2017 00:00:00
const year = moment().add(1, 'year').toString(); // Mon Jan 01 2018 00:00:00

Subtract day, month or year

const day = moment().subtract(1, 'day').toString(); // Sat Dec 31 2016 00:00:00
const month = moment().subtract(1, 'month').toString(); // Thu Dec 01 2016 00:00:00
const year = moment().subtract(1, 'year').toString(); // Fri Jan 01 2016 00:00:00

Difference between two dates

const a = moment('01 Jan 2017');
const b = moment('05 Mar 2018');
// By days
b.diff(a, 'days'); // 428
// By months
b.diff(a, 'month'); // 14
// By years
b.diff(a, 'years'); // 1

BONUS: Check whether its a leap year

const date = moment('2017').isLeapYear(); // false
const date = moment('2020').isLeapYear(); // true

Further Reading

Wow!! That was awesome and easy. Thanks for reading. Do like and share the post and stay tuned for next post.

Không có nhận xét nào:

Cold Turkey Blocker

 https://superuser.com/questions/1366153/how-to-get-rid-of-cold-turkey-website-blocker-get-around-the-block Very old question, but still wan...