0

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?

Rotan075
  • 2,567
  • 5
  • 32
  • 54
  • You don't mention your locale, but 1-1-2017 is pretty unambiguous whilst 22-11-2017 could be being parsed as the 11th day of the 22nd month. Just avoid this date format unless you really, *really* need to, and use yyyy-mm-dd. – Adrian Wragg Nov 25 '16 at 15:03
  • 2
    Your code works fine, the problem is the date format. Your date is `DD-MM-YYYY`. The `Date()` object only accepts `MM-DD-YYYY` or `YYYY-MM-DD` – Rory McCrossan Nov 25 '16 at 15:03
  • http://stackoverflow.com/questions/7151543/convert-dd-mm-yyyy-string-to-date – thekodester Nov 25 '16 at 18:08

2 Answers2

2

The problem is the format of your date string. Date() uses the following format: MM-DD-YYYY. If you change input_date2 to use this format then it works fine.

var input_date = "1-1-2017";
var input_date2 = "11-22-2017";

var myDate = new Date(input_date2);
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>
winseybash
  • 704
  • 2
  • 12
  • 27
-2

This function "filters" the values and add the '0' only if day/month are < 10 (5 to 05 for example) there is plenty of room for improvement... Sorry the first one didn't work at all.

function fixDateFormat(input_date){
        let myDate = new Date(input_date);
        //document.write(myDate + " ----------- ")
        let myDay = myDate.getDate();
        //document.write("Day: " + myDay + "| ");
        let myMonth = myDate.getMonth() + 1;
        //document.write("Month: " + myMonth + "  ::::  ");
        myDay = (myDay < 10) ? ("0" + myDay) : myDay;
        myMonth = (myMonth < 10) ? ("0" + myMonth) : myMonth;
        return myDay + "-" + myMonth + "-" + myDate.getFullYear();
    }


    var input_date = "1-1-2017";
    var input_date2 = "22-11-2017";

    var prettyDate = fixDateFormat(input_date);

    document.write(prettyDate);

I test it and now it return the dd-mm-yyyy format you want.

DIEGO CARRASCAL
  • 1,999
  • 14
  • 16