convert date from dd/mm/yyyy to yyyy-mm-dd in javascript

Okay so I have a table with a column date and date values like ” 2013-05-03 ” since the table is dynamically filled. I have converted the date using php and the date now looks
like ” 03/05/2013″

php conversion:

<td> <?php echo date("d/m/Y", strtotime($row['date'])) ?> </td>

Since I have a edit functionality in my page and I also use
I want to convert the date back to its original format so that chrome could recognize it

var date = new Date(feed date in dd/mm/yyyy format);
    $("#customer_date").val(date);

I tried the above method. But looks like the conversion doesn’t happen.
what could be a different approach?
thanks!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Do a simple split and join

var date = "03/05/2013";
var newdate = date.split("/").reverse().join("-");

"2013-05-03"

Solution 2

As your date is really just a string the quickest way would be to split it like this.

date = "03/05/2013";
date = date.split("/").reverse().join("-");

$("#customer_date").val(date);

Example here http://jsfiddle.net/GNFGP/1

Solution 3

if you want more powerfull date formatting, you can use date format function written by Steven Levithan here

var date = dateFormat(new Date("Thu Oct 14 2010 00:00:00 GMT 0530 (India Standard Time)"), 'yyyy-mm-dd');

Solution 4

‘2015-04-03 16:50:05′ gives ’03-04-2015 16:50’ with below code

var formattedTime = '2015-04-03 16:50:05'.substr(0,10).split('-').reverse().join('-')+" "+'2015-04-03 16:50:05'.substr(11,5);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply