How to work with date format in jquery using moment JS ?

How to work with date format in jquery using moment JS ?

In this article, I will let you know how to work on date manipulation using Javascript jquery with moment library.

Date manipulation is very common process in every website and there are so many tools that you can use to change date format.

Date format can be like mm/dd/yyyy, mm-dd-yyyy, DD-MM-YYYY, yyyy-mm-dd etc.

Using Moment.js, you can easily resolve the issue for date and time format.

There are number of method available with Moment.js like you can add days to current date, subtract days or month from current date etc.

Syntax :
$(document).ready(function() {

    moment(your_string_date).format(here_date_format);

});

This was the example to change date format. Now I will show you how you can add days, subtract days to current date :

moment().add('days', 2);    // adds 2 days to current date
moment().add('months', 2);  // adds 2 months to current date
moment().add('years', 2);   // adds 2 years to current date

Same you can try subtract() method :

moment().subtract('days', 2);   // subtracts 2 days to current date
moment().subtract('months', 2); // subtracts 2 months to current date
moment().subtract('years', 2);  // subtracts 2 years to current date
<!DOCTYPE html>
<html>
<head>
	<title>How to work with date format in jquery using moment JS?</title>
	<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Working on date manipulation:</h2>
  <ul>
    <li>Add : <span id="add_date"></span></li>
    <li>Subtract : <span id="subtract_date"></span></li>

    <li>Nov 17, 2018 : <span id="first_date"></span></li>
    <li>2018-11-17 : <span id="second_date"></span></li>
    <li>2018/11/17 : <span id="third_date"></span></li>
   
  </ul>
</div>

</body>

<script type="text/javascript">
  $(document).ready(function() {

    var add_day=moment().add('days', 2);
    $("#add_date").text(add_day);

    var subtract_day=moment().subtract('days', 2);  
    $("#subtract_date").text(subtract_day);


    var first_date = moment("Nov 17, 2018").format('DD-MM-YYYY');
    $("#first_date").text(first_date);


    var second_date = moment("2018-11-17").format('DD-MM-YYYY');
    $("#second_date").text(second_date);


    var third_date = moment("2018/11/17").format('DD/MM/YYYY');
    $("#third_date").text(third_date);


  });
</script>
</html>

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.