Javascript 将 yyyy-mm-dd hh:mm:ss 转换为 newDate("day, month date year hh:mm:ss")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22959817/
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
Javascript convert yyyy-mm-dd hh:mm:ss like newDate("day, month date year hh:mm:ss")
提问by user3484934
I want to use Date Object Methods(getHoures(), getSeconds()
...) on new Date("string") (string = month day, year hh:mm:ss
) but my date format is like this : yyyy-mm-dd hh:mm:ss
我想在) 上使用日期对象方法( getHoures(), getSeconds()
... new Date("string") (string = month day, year hh:mm:ss
) 但我的日期格式是这样的:yyyy-mm-dd hh:mm:ss
So I need to convert "yyyy-mm-dd hh:mm:ss"
to "("day, month date year hh:mm:ss"
)"
所以我需要转换"yyyy-mm-dd hh:mm:ss"
为“( "day, month date year hh:mm:ss"
)”
How can i do please ?
请问我该怎么办?
Thx !
谢谢 !
回答by Anoop
Try this: JSFIDDLEthere are many JavaScript libraries available. format 'yyyy-mm-dd hh:mm:ss' used by MySQL server. you can covert above string into date object using following code:
试试这个:JSFIDDLE有许多可用的 JavaScript 库。MySQL 服务器使用的格式“yyyy-mm-dd hh:mm:ss”。您可以使用以下代码将上述字符串转换为日期对象:
var arr = " yyyy-mm-dd hh:mm:ss".split(/-|\s|:/);// split string and create array.
var date = new Date(arr[0], arr[1] -1, arr[2], arr[3], arr[4], arr[5]); // decrease month value by 1
回答by Alnitak
Actually, you don't need to perform any such conversion.
实际上,您不需要执行任何此类转换。
The string you have is already in very nearlyan acceptable format for new Date(string)
and in fact even as is will be accepted by most (if not all) modern browsers.
您拥有的字符串已经非常接近可接受的格式new Date(string)
,事实上即使是大多数(如果不是全部)现代浏览器也会接受。
To make it fully compliant with the ISO8601 variant that ES5 mandates, just replace the space between the date and time with a literal T
character, and add a time-zone specifier at the end (e.g. either Z
for UTC, or +01:00
, for example).
为了使其完全符合 ES5 规定的 ISO8601 变体,只需用文字T
字符替换日期和时间之间的空格,并在末尾添加时区说明符(例如,Z
对于 UTC 或+01:00
)。
See http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15for more.
有关更多信息,请参阅http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15。
回答by Modi Mohammed
Try this:
试试这个:
d = Date().split(" ");
var date = d[2] + "-" + d[1] + "-" + d[3] + " " + d[4]
d = Date().split(" ");
var date = d[2] + "-" + d[1] + "-" + d[3] + " " + d[4]