0

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 year part: 20 as "06/28/20", after I used new Date(startDate), the result is "Mon Jun 28 1920 00:00:00 GMT+0530 (India Standard Time)".

After this when I applied another format on "06/28/20":

startDate = moment(startDate ).format('MM-DD-YYYY');

The result is 06-28-1920

In Google Chrome and Firefox it gives the correct date for the second attempt as: 06-28-2020.

My code is:

$(document).ready(function() {

var startDate = moment(new Date()).format('MM/DD/YY');
alert("startDate ==="+startDate +"==="+new Date(startDate ));

startDate = moment(startDate ).format('MM-DD-YYYY');
alert("startDate ==="+startDate +"==="+new Date(startDate ));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Prabha
  • 266
  • 4
  • 25

2 Answers2

1

Please find the below snippet , which might help you

$(document).ready(function() {

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
Dilip D
  • 567
  • 1
  • 5
  • 28
  • 1
    Hi @Dilip, thanks for the details, here the issue is different which is related to momentjs format method usage as clarified by alpsavrum. – Prabha Jun 28 '20 at 16:08
0

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.

savrum
  • 203
  • 1
  • 6
  • Hi @alpsavrum, please explain a little bit more on this, moment.format(dateformat) behaves differently in chrome also for different dateformats 'YY-DD-MMM', 'DD-MM-YY','MM/DD/YY' . – Prabha Jun 28 '20 at 12:56
  • moment.format is not the right use. use it like moment().format(dateformat). you should construct the moment before using it. – savrum Jun 28 '20 at 12:58
  • I converted the line var startDate = moment(new Date()).format('MM/DD/YY') to var startDate1 = moment(new Date(),'MM/DD/YY'), It is working fine in all browsers. Are you suggesting the same, please confirm. – Prabha Jun 28 '20 at 13:00
  • Hi @alpsavrum, moment(new Date()).format('YYYY/MM/DD') results as 2020/06/28(as required format in output) As per the suggestion I replaced the above line with as: moment(new Date() ,'YYYY/MM/DD'), but it results 1593368656727, how can we get this value as format 'YYYY/MM/DD' – Prabha Jun 28 '20 at 18:28