简体   繁体   English

将格式为YYYYMMDDHHMMSS的字符串转换为JavaScript Date对象

[英]Converting a string formatted YYYYMMDDHHMMSS into a JavaScript Date object

I have a string with a date in it formatted like so: YYYYMMDDHHMMSS. 我有一个字符串,其日期格式如下:YYYYMMDDHHMMSS。 I was wondering how I would convert it into a JavaScript Date object with JavaScript. 我想知道如何使用JavaScript将其转换为JavaScript Date对象。

Thanks in advance! 提前致谢!

How about this for a wacky way to do it: 这是一个古怪的方法来做到这一点:

var date = new Date(myStr.replace(
    /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
    '$4:$5:$6 $2/$3/$1'
));

Zero external libraries, one line of code ;-) 零外部库,一行代码;-)


Explanation of the original method : 原方法说明:

// EDIT: this doesn't work! see below.
var date = Date.apply(
    null,
    myStr.match(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/).slice(1)
);

The match() function (after it has been slice() d to contain just the right) will return an array containing year, month, day, hour, minute, and seconds. match()函数(在slice() d之后包含右侧)将返回一个包含年,月,日,小时,分钟和秒的数组。 This just happens to be the exact right order for the Date constructor. 这恰好是Date构造函数的正确顺序。 Function.apply is a way to call a function with the arguments in an array, so I used Date.apply(<that array>) . Function.apply是一种使用数组中的参数调用函数的方法,因此我使用Date.apply(<that array>)

for example: 例如:

var foo = function(a, b, c) { };

// the following two snippets are functionally equivalent
foo('A', 'B', 'C')

var arr = ['A', 'B', 'C'];
foo.apply(null, arr);

I've just now realised that this function doesn't actually work, since javascript months are zero-indexed. 我刚刚意识到这个函数实际上并不起作用,因为javascript月份是零索引的。 You could still do it in a similar method, but there'd be an intermediate step, subtracting one from the array before passing it to the constructor. 你仍然可以用类似的方法来做,但是有一个中间步骤,在将数据传递给构造函数之前从数组中减去一个。 I've left it here, since it was asked about in the comments. 我把它留在了这里,因为它在评论中被问到了。

The other option works as expected however. 另一种选择按预期工作。

Primitive version: 原始版本:

new Date(foo.slice(0, 4), foo.slice(4, 6) - 1, foo.slice(6, 8),
    foo.slice(8, 10), foo.slice(10, 12), foo.slice(12, 14))

An explicit conversion of the strings to numbers is unnecessary: the Date() function will do this for you. 不需要将字符串显式转换为数字: Date()函数将为您执行此操作。

I find DateJS the best library for anything that has to do with converting, parsing and using dates in JavaScript. 我发现DateJS是与JavaScript中的转换,解析和使用日期有关的任何事情的最佳库。 If you need that kind of conversions more often you should really consider using it in your application: 如果您需要更频繁地进行此类转换,则应该考虑在应用程序中使用它:

Date.parseExact("20091202051200", "YYYYMMDDHHMMSS");

如果你可以使用jQuery,jQuery.ui.datepicker有个实用的功能

Date functions from this library can help: http://javascripttoolbox.com/lib/date/ , it's free and very simple to use. 此库中的日期函数可以提供帮助: http//javascripttoolbox.com/lib/date/ ,它是免费的,使用起来非常简单。

If you want to do it yourself, I would suggest looking up the javascript regular expressions, as I guess that is the way to achieve this best 如果你想自己做,我建议查找javascript正则表达式,因为我猜这是实现这一目标的最佳方式

There is no time zone information in your string. 您的字符串中没有时区信息。 It most likely is GMT, so that should be accounted for when you make the Date. 它很可能是格林威治标准时间,因此在您制作日期时应考虑到这一点。 You can easily do the conversion with simple javascript methods. 您可以使用简单的JavaScript方法轻松完成转换。

Date.Brit= (function(){
    return Date.parse('2/6/2009')> Date.parse('6/2/2009');
})()


var s= "20091202093000"
var D= s.match(/(\d{2})/g);
if(Date.Brit){
    D.splice(2, 2, D[3],D[2]);
}
var day= new Date(Date.parse(D[2]+'/'+D[3]+'/'+
D[0]+D[1]+' '+D.splice(4).join(':')+' GMT'));

day.toUTCString()+'\n'+day


/*  returned value: (String)
Wed, 02 Dec 2009 09:30:00 GMT
Wed Dec 02 2009 04:30:00 GMT-0500 (Eastern Standard Time)
*/

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

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