Javascript日期时间字符串到Date对象 [英] Javascript datetime string to Date object

查看:80
本文介绍了Javascript日期时间字符串到Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试具有某些功能的小型应用程序,该功能只能在Chrome中运行。问题出在日期选择器中,您可以在其中选择日期和时间,而日期选择器将日期和时间限定为日期时间字符串。

I am debugging a small application with some functionality which would only run in Chrome. The problem lies in a datepicker where you choose a date and time and the datepicker concaternates it into a datetime-string.

无论如何,字符串看起来像这样: 2012-10-20 00:00

Anyway the string looks like this: 2012-10-20 00:00.

但是,使用它的javascript现在只接受字符串并使用像这样: new Date('2012-10-20 00:00');

However, the javascript that uses it now just takes the string and initialize an object with it like this: new Date('2012-10-20 00:00');

在Firefox,IE和除Chrome之外的所有浏览器中都是无效的日期。
我需要如何最好地将日期字符串转换为javascript中的Date对象的建议。我启用了jQuery。

This is resulting in an invalid date in Firefox, IE and probably all browsers but Chrome. I need advise in how I best could transform this datestring to a Date object in javascript. I have jQuery enabled.

感谢您的贤哲建议和更明智的选择。

Thanks for your sage advise and better wisdom.

推荐答案

如果字符串格式始终如您所述,请分割字符串并使用位,例如:

If the string format is always as you state, then split the string and use the bits, e.g.:

var s = '2012-10-20 00:00';
var bits = s.split(/\D/);
var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4]);

这篇关于Javascript日期时间字符串到Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆