php 在javascript中将日期从dd/mm/yyyy转换为yyyy-mm-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19709793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert date from dd/mm/yyyy to yyyy-mm-dd in javascript
提问by user2933671
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!
我试过上面的方法。但看起来转换不会发生。什么是不同的方法?谢谢!
回答by Andrei Nemes
Do a simple split and join
做一个简单的拆分和加入
var date = "03/05/2013";
var newdate = date.split("/").reverse().join("-");
"2013-05-03"
回答by Dominic Green
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
回答by AmanS
if you want more powerfull date formatting, you can use date format function written by Steven Levithanhere
如果你想要更强大的日期格式,你可以在这里使用Steven Levithan编写的日期格式函数
var date = dateFormat(new Date("Thu Oct 14 2010 00:00:00 GMT 0530 (India Standard Time)"), 'yyyy-mm-dd');
回答by Günes Erdemi
'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);