7

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!

cimmanon
  • 67,211
  • 17
  • 165
  • 171
user2933671
  • 273
  • 3
  • 5
  • 19
  • What did you do at ‘feed date in dd/mm/yyyy format’? – Marcel Korpel Oct 31 '13 at 14:57
  • Working with dates in JavaScript is pretty shitty, You don't have a template tool or anything like you do in eq. PHP. I use moment alot: http://momentjs.com/ – Andreas Louv Oct 31 '13 at 14:57
  • Do not work directly with dates. Do not split them and do not parse them unless you're really sure of what you're doing. Take a look http://stackoverflow.com/a/17064680/1207195 for some hints. – Adriano Repetti Oct 31 '13 at 14:57
  • 1
    I would not bother, you will have to do server-side validation anyway so I would do it all there. Although I am not sure what you mean with `so that chrome could recognize it`... – jeroen Oct 31 '13 at 15:03

4 Answers4

59

Do a simple split and join

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

"2013-05-03"
Andrei Nemes
  • 3,062
  • 1
  • 16
  • 22
  • Only if his **customers** will **accept dates in that format** (only) and he has never to **convert** that string **to a date object**. It **may** satisfy his requirements but it should be pointed out... – Adriano Repetti Oct 31 '13 at 15:04
  • So simple! Thank you so much (even 3 years after your answer)! – William Jun 02 '16 at 12:56
  • Glad you found it useful! As others have pointed out you have to be careful that your date is in the right format since you are making that assumption :) – Andrei Nemes Jun 02 '16 at 14:11
  • Great answer, What about the reverse? I mean if we have date 2013-05-03 to 03-05-2013? – user9437856 Aug 02 '18 at 12:47
4

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

Dominic Green
  • 10,142
  • 4
  • 30
  • 34
1

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');
AmanS
  • 1,490
  • 2
  • 13
  • 22
0

'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);
Günes Erdemi
  • 71
  • 1
  • 2