Moment.js Javascript Date Time Manipulation Library Full Crash Course with Practical Examples

Moment.js Javascript Date Time Manipulation Library Full Crash Course with Practical Examples

 

Welcome folks today in this blog post we will be talking about moment.js which is a date time manipulation library written in javascript. We will be talking in detail with practical examples about this library. All the source code of examples will be given below. Let’s get Started

 

 

Get Started

 

 

In order to get started you need to visit the official site of moment.js and download the library files and install it inside your root directory or you can include it from the cdn

 

 

Moment.js CDN

 

 

You can also get started by adding the moment.js library by including a CDN inside your html page and then you can use moment inside your javascript.

 

 

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

 

 

 

Installing it By NPM

 

 

You can also use moment library inside your node.js application. You just need to execute the below command to install moment as a node dependency

 

npm i moment

 

 

Basic Example

 

 

Now to use the moment library and have a basic example of initializing a new date in moment

 

var date = moment()

console.log(date.fromNow())

 

See also  Build a Node.js XML Document to JSON Object Parser or Converter Using xml2json Library in TypeScript Full Project For Beginners

 

 

 

Now you can see that in moment we have initialized a date and this has a special method fromNow() which returns the current time

 

Adding Days to Current Time

 

Now we will look at a example where we will add days to the current date and time in moment.js

 

const today = moment();
const nextWeek = today.clone().add(7, 'days');

$("#result").html(nextWeek.format('dddd Do MMMM, YYYY'));

 

 

 

In same way we can add minutes hours years and seconds to the current date and time in moment

 

const today = moment();
const nextWeek = today.clone().add(7, 'years');
const nextWeek = today.clone().add(7, 'hours');
const nextWeek = today.clone().add(7, 'minutes');
const nextWeek = today.clone().add(7, 'seconds');

 

 

Subtracting Days From Current Date and Time

 

 

Now we will be looking at an example where we will be subtracting days,hours,minutes and seconds from the current date and time in moment.

 

const today = moment();
const nextWeek = today.clone().subtract(7, 'years');
const nextWeek = today.clone().subtract(7, 'hours');
const nextWeek = today.clone().subtract(7, 'minutes');
const nextWeek = today.clone().subtract(7, 'seconds');

 

 

Difference Between Two Dates

 

 

Now in this examples we will be seeing how to calculate the difference between two dates in moment.js

See also  PM2 Node.js Library to Run Node Applications Forever With Built in Load Balancer Full Tutorial For Beginners

 

const dateB = moment('2014-11-11');
const dateC = moment('2014-10-11');

console.log('Difference is ', dateB.diff(dateC), 'milliseconds');
console.log('Difference is ', dateB.diff(dateC, 'days'), 'days');
console.log('Difference is ', dateB.diff(dateC, 'months'), 'months');

 

 

 

 

So now you can see in the examples we are using the diff() method to calculate the difference or subtract two dates in moment.js

 

 

Date Comparisons

 

 

In order to compare two dates in moment.js we have some useful methods which are namely isBefore()isAfter(), and isSame()

 

 

console.log(moment('2020-01-01').isAfter('2019-01-01')); // true
console.log(moment('2020-01-01').isAfter('2020-01-08')); // false

 

 

 

Checking Leap Year in Moment

 

 

In order to check whether a year is leap or not we have a special method isLeapYear() in moment.js.

 

 

console.log(moment([2020]).isLeapYear()); // true
console.log(moment([2019]).isLeapYear()); // false

 

 

 

Date Validation

 

 

In order to validate dates inside your application in moment.js look at these examples

 

 

console.log(moment("2020-01-01", "YYYY-MM-DD").isValid()); // true
console.log(moment("not-a-date", "YYYY-MM-DD").isValid()); // false

 

 

 

Node.js Example

 

 

At last we will be looking a node.js example where we will be using moment library.

See also  React.js Tutorial to Print HTML Components Using react-easy-print Library in Browser Using Javascript Full Project For Beginners

 

 

const moment = require('moment');
const today = moment();
const nextWeek = today.add(7, 'days');
console.log(today.fromNow());

 

 

Leave a Reply