简体   繁体   English

在JavaScript中,如何将日期时间值从一种格式转换为另一种格式?

[英]In JavaScript, How to Convert a date time value from one format to the other?

Please look at the format of these date time values: 请查看以下日期时间值的格式:

var from = "2012-01-13 T11:00:00";
var to = "13 Jan 2012 11:00am";

In the above example, how to write a javascript function which converts a value in "from" format to a value in "to" format, both are datetime values in text obvioulsy. 在上面的示例中,如何编写Javascript函数将“从”格式的值转换为“至”格式的值,这两个都是文本无效的日期时间值。

var to = convertDateTime(from);
function convertDateTime(from)
{
// how to implement this?
}

Many thanks, 非常感谢,

look at this article : http://blog.stevenlevithan.com/archives/date-time-format 看这篇文章: http : //blog.stevenlevithan.com/archives/date-time-format

you must convert your string object into date type and then retransform it to string using date format 您必须将字符串对象转换为日期类型,然后使用日期格式将其重新转换为字符串

You can use a library like XDate that will handle this for you. 您可以使用像XDate这样的库来为您处理。

There is no need to reinvent the wheel. 无需重新发明轮子。 When working with dates, you can run into several browser inconsistencies. 处理日期时,可能会遇到几种浏览器不一致的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM