我有字符串格式的日期,有时是这样的:05-11-2009 16:59:20
有时是这样的:2013-12-05T22:00:00:00Z
而其他一些时间是这样的:2013-12-05T22:00:00:00.000Z
。
我编写了一个从这些日期中提取月日和年的函数,但我想要一个可以为所有输入格式的函数工作的函数。就像是:
function DateParts(datetime, format) {
var matches = datetime.splitByFormat(format);
this.getYear = function () {
return matches[3];
};
this.getMonth = function () {
return (matches[2] - 1) +"" ;
};
this.getDay = function () {
return matches[1];
};
this.getHours = function () {
return matches[4];
};
this.getMinutes = function () {
return matches[5];
};
this.getSeconds = function () {
return matches[6];
};
};
格式将是什么"yyyy-mm-dd hh:MM:ss"
或"dd-mm-yyyyThh:MM:ss.dddZ"
什么。
有没有一种很好的方法来创建 splitByFormat 函数而不必把我的头弄掉?