且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript将日期时间字符串转换为***

更新时间:2023-02-03 16:40:50

JavaScript日期在内存中以毫秒为单位。您只需要将其转换为数字,例如与一元的 + 运算符,以获得它们。或者您可以使用 .getTime 方法

JavaScript dates are internally stored as milliseconds since epoch. You just need to convert it to a number, e.g. with the unary + operator, to get them. Or you can use the .getTime method.

更难的是解析你的日期字符串。您可能会使用正则表达式从字符串中提取值,并将其传递到 Date.UTC

The harder will be parsing your date string. You likely will use a regex to extract the values from your string and pass them into Date.UTC:

var parts = datestring.match(/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2})/);
return Date.UTC(+parts[3], parts[2]-1, +parts[1], +parts[4], +parts[5]);

这将为您的示例生成 1354604400000 ms 日期。

This will yield 1354604400000 ms for your example date.