Skip to content Skip to sidebar Skip to footer

Date Went Wrong When Using Momentjs Date Format In Ie 11

I am using Moment.js format function on a current date as var startDate = moment(new Date()).format('MM/DD/YY'); The result is 06/28/20 What happens is that it retains only the ye

Solution 1:

Please find the below snippet , which might help you

$(document).ready(function() {

  var startDate = moment(newDate()).format('MM/DD/YY');
  alert("startDate ==="+startDate +"==="+newDate(startDate ));
  
  startDate = moment(newDate(startDate)).format('MM-DD-YYYY');
  var str="06-28-2020";
  
  
  alert("startDate ==="+startDate +"==="+newDate(str.replaceAll("-","/")));
  
  });
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>

Solution 2:

If you are using Moment, use Moment only. Parsing date strings with Date.parse() or new Date() (same thing under hood) is not recommended.

Your approach should be using moment(DATE, "DD/MM/YY") which handles the 2k year problem you face, automatically.

Post a Comment for "Date Went Wrong When Using Momentjs Date Format In Ie 11"