在javascript中将日期从dd/mm/yyyy转换为yyyy-mm-dd

时间:2022-10-29 13:01:26

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"

我有一个表,其中有一个列日期和日期值,比如“2013-05-03”,因为该表是动态填充的。我已经用php转换了日期,现在日期看起来是“03/05/2013”

php conversion:

php的转换:

<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

因为我有一个编辑功能在我的页面,我也使用我想转换日期回到它的原始格式,以便chrome可以识别它

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!

我尝试了上面的方法。但是看起来这种转换不会发生。还有什么不同的方法呢?谢谢!

4 个解决方案

#1


46  

Do a simple split and join

做一个简单的分割和加入

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

"2013-05-03"

#2


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

例子http://jsfiddle.net/GNFGP/1

#3


1  

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

如果您想要更多的powerfull日期格式,可以使用Steven Levithan在这里编写的日期格式函数

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

#4


0  

'2015-04-03 16:50:05' gives '03-04-2015 16:50' with below code

“2015-04-03 16:50:05”给出“03-04-2015 16:50”,代码如下

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

#1


46  

Do a simple split and join

做一个简单的分割和加入

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

"2013-05-03"

#2


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

例子http://jsfiddle.net/GNFGP/1

#3


1  

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

如果您想要更多的powerfull日期格式,可以使用Steven Levithan在这里编写的日期格式函数

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

#4


0  

'2015-04-03 16:50:05' gives '03-04-2015 16:50' with below code

“2015-04-03 16:50:05”给出“03-04-2015 16:50”,代码如下

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