如何在 JavaScript 中以“YYYYmmdd”格式解析日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10638529/
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 parse a date in format "YYYYmmdd" in JavaScript?
提问by Michael
This is a noob question:
这是一个菜鸟问题:
How to parse a date in format "YYYYmmdd"
without external libraries ? If the input string is not in this format I would like to get invalid Date
(or undefined if it will be easier).
如何在"YYYYmmdd"
没有外部库的情况下以格式解析日期?如果输入字符串不是这种格式,我想得到invalid Date
(或者 undefined 如果它更容易的话)。
回答by Parth Thakkar
function parse(str) {
if(!/^(\d){8}$/.test(str)) return "invalid date";
var y = str.substr(0,4),
m = str.substr(4,2),
d = str.substr(6,2);
return new Date(y,m,d);
}
Usage:
用法:
parse('20120401');
UPDATE:
更新:
As Rocket said, months are 0-based in js...use this if month's aren't 0-based in your string
正如 Rocket 所说,月份在 js 中是基于 0 的……如果月份在您的字符串中不是基于 0 的,请使用它
function parse(str) {
if(!/^(\d){8}$/.test(str)) return "invalid date";
var y = str.substr(0,4),
m = str.substr(4,2) - 1,
d = str.substr(6,2);
return new Date(y,m,d);
}
UPDATE:
更新:
More rigorous checking for validity of date. Adopted HBP's way to validate date.
更严格地检查日期的有效性。采用 HBP 的方式来验证日期。
function parse(str) {
var y = str.substr(0,4),
m = str.substr(4,2) - 1,
d = str.substr(6,2);
var D = new Date(y,m,d);
return (D.getFullYear() == y && D.getMonth() == m && D.getDate() == d) ? D : 'invalid date';
}
回答by HBP
A more robust version validating the numbers :
验证数字的更强大的版本:
function parse (str) {
// validate year as 4 digits, month as 01-12, and day as 01-31
if ((str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$/))) {
// make a date
str[0] = new Date (+str[1], +str[2] - 1, +str[3]);
// check if month stayed the same (ie that day number is valid)
if (str[0].getMonth () === +str[2] - 1)
return str[0];
}
return undefined;
}
See fiddle at : http://jsfiddle.net/jstoolsmith/zJ7dM/
见小提琴:http: //jsfiddle.net/jstoolsmith/zJ7dM/
I recently wrote a much more capable version you can find here : http://jsfiddle.net/jstoolsmith/Db3JM/
我最近写了一个功能更强大的版本,你可以在这里找到:http: //jsfiddle.net/jstoolsmith/Db3JM/
回答by MrE
simplistic answer maybe, without checks, but fast...
简单的答案也许没有检查,但很快......
var date = parseInt(date);
new Date(date / 10000, date % 10000 / 100, date % 100);
or, if months are not zero based in the source,
或者,如果基于源的月份不为零,
new Date(date / 10000, (date % 10000 / 100) - 1, date % 100);
回答by Or Gal
combining HBP's answer and this answerto get a function that parses YYYYMMDDHHmm and here is a fiddle
结合 HBP 的答案和这个答案来得到一个解析 YYYYMMDDHHmm 的函数,这里是一个小提琴
var parseTS=function(str){
// validate year as 4 digits, month as 01-12, and day as 01-31
if ((str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])([0-5]\d)$/))) {
// make a date
str[0] = new Date (+str[1], +str[2] - 1, +str[3], +str[4], +str[5]);
// check if month stayed the same (ie that day number is valid)
if (str[0].getMonth () === +str[2] - 1) {
return str[0];
}
}
return undefined;
};
console.log(parseTS('201501012645'));