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>