Hello I got a question regarding formatting a date where the days and months have a leading zero if the date does not have a leading zero, for example in cases of: 1-1-2017.
Now I tried to code some code based on different answers here on Stackoverflow but without succes.
Here is my code:
var input_date = "1-1-2017";
var input_date2 = "22-11-2017";
var myDate = new Date(input_date);
var prettyDate =
( '0' + (myDate.getDate()) ).slice( -2 ) + '-' +
( '0' + (myDate.getMonth()+1) ).slice( -2 ) + '-' +
myDate.getFullYear();
document.write( prettyDate );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
and this works for dates as: 1-1-2017 but whenever the variable input_date2
is used as Date parameter then the formatting goes wrong.
I am looking for a mechanism which formats my date if the leading zero is missing. Any help would be appreciate it.
PS. Actually, I was wondering would this not be easier with jQuery instead of using plain Javascript as I did above?