Download
User Rating: 0/5 ( 0 votes)
Features/Benefits:
-
It supports a lot of international languages.
-
Object mutability.
-
Multiple internal properties like epoch shifting, retrieving native Date objects, etc.
-
To use its parser correctly, there are some guidelines such as strict mode, date formats, forgiving mode, etc.
Use cases:
-
To display the time in a published article.
-
Communicating with people from across the world in their local language.
Source: momentjs.com
1. Install
npm install moment --save # npm
yarn add moment # Yarn
Install-Package Moment.js # NuGet
spm install moment --save # spm
meteor add momentjs:moment # meteor
bower install moment --save # bower (deprecated)
2. Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // October 6th 2022, 12:21:57 pm
moment().format('dddd'); // Thursday
moment().format("MMM Do YY"); // Oct 6th 22
moment().format('YYYY [escaped] YYYY'); // 2022 escaped 2022
moment().format(); // 2022-10-06T12:21:57+07:00
3. Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 11 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 10 years ago
moment().startOf('day').fromNow(); // 12 hours ago
moment().endOf('day').fromNow(); // in 12 hours
moment().startOf('hour').fromNow(); // 22 minutes ago
4. Calendar Time
moment().subtract(10, 'days').calendar(); // 09/26/2022
moment().subtract(6, 'days').calendar(); // Last Friday at 12:22 PM
moment().subtract(3, 'days').calendar(); // Last Monday at 12:22 PM
moment().subtract(1, 'days').calendar(); // Yesterday at 12:22 PM
moment().calendar(); // Today at 12:22 PM
moment().add(1, 'days').calendar(); // Tomorrow at 12:22 PM
moment().add(3, 'days').calendar(); // Sunday at 12:22 PM
moment().add(10, 'days').calendar(); // 10/16/2022
5. Multiple Locale Support
moment.locale(); // en
moment().format('LT'); // 12:23 PM
moment().format('LTS'); // 12:23:25 PM
moment().format('L'); // 10/06/2022
moment().format('l'); // 10/6/2022
moment().format('LL'); // October 6, 2022
moment().format('ll'); // Oct 6, 2022
moment().format('LLL'); // October 6, 2022 12:23 PM
moment().format('lll'); // Oct 6, 2022 12:23 PM
moment().format('LLLL'); // Thursday, October 6, 2022 12:23 PM
moment().format('llll'); // Thu, Oct 6, 2022 12:23 PM