如何在 jQuery 中转换为日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11482723/
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
how to convert to datetime in jQuery?
提问by Ram Singh
I want to convert String value to datetime. Actually i have a String value( That is date) getting it from back end. And Now I want to convert it to datetime as i have to apply countdown jquery plugin for that value. And Count Down plugin takes a "Untill" value as parameter. So for that i want to convert it to datetime.I have tried the following code referencesJquery Function To convert Datetime,
我想将字符串值转换为日期时间。实际上我有一个字符串值(即日期)从后端获取它。现在我想将它转换为日期时间,因为我必须为该值应用倒计时 jquery 插件。Count Down 插件将“ Untill”值作为参数。因此,我想将其转换为日期时间。我尝试了以下代码引用Jquery 函数来转换日期时间,
var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));
But its giving me error "Timestamp: 7/14/2012 3:16:05 PM Error: radix must be an integer at least 2 and no greater than 36". Please help me come out from this problem.
但它给了我错误“时间戳:7/14/2012 3:16:05 PM 错误:基数必须是至少为 2 且不大于 36 的整数”。请帮我解决这个问题。
回答by SALMAN
Theres a getDateFromFormat() function that you can tweak a little to solve your problem
有一个 getDateFromFormat() 函数,你可以稍微调整一下来解决你的问题
OR
或者
var sDate = new Date(Date.parse("XXX","ZZZ"));
Where XXX is your Date string. And ZZZ is your format for XXX.
其中 XXX 是您的日期字符串。ZZZ 是您的 XXX 格式。
i.e.
IE
If I want to convert string "03/20/2012" which is in "MM/dd/yyyy" format then
如果我想转换“MM/dd/yyyy”格式的字符串“03/20/2012”,那么
var sDate = new Date(Date.parse("03/20/2012","MM/dd/yyyy"));