Javatpoint Logo
Javatpoint Logo

Use of moment JS to change date format in jquery

Many times we are required to change the format of date like DD-MM-YYYY, mm/dd/yyyy, yyyy-mm-dd, mm-dd-yyyy, etc into jquery. In that case, we can use moment jquery library. Now we will see the working of it in the following example. The use of it is fantastic and very easy.

The jQuery plugin is described by the moment.js, which will provide the ability to change the format of date from string date. It will also allow us to compare dates where we can compare the two dates etc. While working with dates, we can do many things like carbon. So if we want to use jquery in our program, we should be use moment js plugin so that we can change the date format.

When we use Moment js, it will become very easy for us to change the string date into the specific format of date. In order to change the date format, the following syntax can be used:

Syntax

Now we will provide a simple example in which we are giving three dates in different formats, and then we will convert it into the format "DD-MM-YYYY". The example is given as follows:

  1. May 16, 2020 : 16-05-2020
  2. 2020-05-16 : 16-05-2020
  3. 2020/05/16 : 16/05/2020

In the above example, we can see the basic string of dates. So we can check the dates by passing the date string. The working of it is explained by the following full example.

index.html

Now the above example is ready to run. After executing the above example, we will get the following output:

Examples of Date format:

  • May 16, 2020 : 16-05-2020
  • 2020-05-16 : 16-05-2020
  • 2020/05/16 : 16/05/2020





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA