需要有关将具有以下结构的ISO 8601日期转换为javascript的帮助/提示。
CCYY-MM-DDThh:mm:ssTZD
我想像这样对比这个日期:
January 28, 2011 - 7:30PM EST
我希望尽可能保持这种解决方案的清洁和最小化。
答案 0 :(得分:57)
Date对象将8601作为它的第一个参数:
var d = new Date("2014-04-07T13:58:10.104Z");
console.log(d.toString());
答案 1 :(得分:26)
datejs可以解析以下内容,您可能想尝试一下。
Date.parse('1997-07-16T19:20:15') // ISO 8601 Formats
Date.parse('1997-07-16T19:20:30+01:00') // ISO 8601 with Timezone offset
修改:正则表达式
x = "2011-01-28T19:30:00EST"
MM = ["January", "February","March","April","May","June","July","August","September","October","November", "December"]
xx = x.replace(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}(\w{3})/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$2-1]+" "+$3+", "+$1+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
结果
January 28, 2011 - 7:30PM EST
编辑2:我将时区更改为EST,现在我已关注
x = "2011-01-28T19:30:00-05:00"
MM = {Jan:"January", Feb:"February", Mar:"March", Apr:"April", May:"May", Jun:"June", Jul:"July", Aug:"August", Sep:"September", Oct:"October", Nov:"November", Dec:"December"}
xx = String(new Date(x)).replace(
/\w{3} (\w{3}) (\d{2}) (\d{4}) (\d{2}):(\d{2}):[^(]+\(([A-Z]{3})\)/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$1]+" "+$2+", "+$3+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
返回
January 28, 2011 - 7:30PM EST
基本上
String(new Date(x))
返回
Fri Jan 28 2011 19:30:00 GMT-0500 (EST)
正则表达式部分只是将上面的字符串转换为您所需的格式。
January 28, 2011 - 7:30PM EST
答案 2 :(得分:11)
如果你想保持简单,这应该足够了:
function parseIsoDatetime(dtstr) {
var dt = dtstr.split(/[: T-]/).map(parseFloat);
return new Date(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0, 0);
}
注意 parseFloat是必须的,parseInt并不总是有效。地图需要IE9或更高版本。
适用于格式:
对时区无效,请参阅其他答案。
答案 3 :(得分:2)
也许,你可以使用moment.js,在我看来这是最好的JavaScript库,用于解析,格式化和使用日期客户端。你可以使用类似的东西:
var momentDate = moment('1890-09-30T23:59:59+01:16:20', 'YYYY-MM-DDTHH:mm:ss+-HH:mm:ss');
var jsDate = momentDate.toDate();
// Now, you can run any JavaScript Date method
jsDate.toLocaleString();

使用像moment.js这样的库的好处是,即使在IE 8 +等传统浏览器中,您的代码也能完美运行。
以下是解析方法的文档:https://momentjs.com/docs/#/parsing/
答案 4 :(得分:0)
根据MSDN,JavaScript Date对象不提供任何特定的日期格式化方法(正如您可能在其他编程语言中看到的那样)。但是,您可以使用一些Date
方法和格式来实现目标:
function dateToString (date) {
// Use an array to format the month numbers
var months = [
"January",
"February",
"March",
...
];
// Use an object to format the timezone identifiers
var timeZones = {
"360": "EST",
...
};
var month = months[date.getMonth()];
var day = date.getDate();
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var time = (hours > 11 ? (hours - 11) : (hours + 1)) + ":" + minutes + (hours > 11 ? "PM" : "AM");
var timezone = timeZones[date.getTimezoneOffset()];
// Returns formatted date as string (e.g. January 28, 2011 - 7:30PM EST)
return month + " " + day + ", " + year + " - " + time + " " + timezone;
}
var date = new Date("2011-01-28T19:30:00-05:00");
alert(dateToString(date));
您甚至可以更进一步,并覆盖Date.toString()
方法:
function dateToString () { // No date argument this time
// Use an array to format the month numbers
var months = [
"January",
"February",
"March",
...
];
// Use an object to format the timezone identifiers
var timeZones = {
"360": "EST",
...
};
var month = months[*this*.getMonth()];
var day = *this*.getDate();
var year = *this*.getFullYear();
var hours = *this*.getHours();
var minutes = *this*.getMinutes();
var time = (hours > 11 ? (hours - 11) : (hours + 1)) + ":" + minutes + (hours > 11 ? "PM" : "AM");
var timezone = timeZones[*this*.getTimezoneOffset()];
// Returns formatted date as string (e.g. January 28, 2011 - 7:30PM EST)
return month + " " + day + ", " + year + " - " + time + " " + timezone;
}
var date = new Date("2011-01-28T19:30:00-05:00");
Date.prototype.toString = dateToString;
alert(date.toString());
答案 5 :(得分:0)
看起来 moment.js 是最受欢迎且开发积极的:
moment("2010-01-01T05:06:07", moment.ISO_8601);