在 Javascript 中解析 ISO 8601 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4829569/
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
Parsing ISO 8601 date in Javascript
提问by Slythic
Need help/tips on converting an ISO 8601 date with the following structure into javascript.
需要将具有以下结构的 ISO 8601 日期转换为 javascript 的帮助/提示。
CCYY-MM-DDThh:mm:ssTZD
I'd like to format the date like so:
我想像这样格式化日期:
January 28, 2011 - 7:30PM EST
I'd like to keep this solution as clean and minimal as possible.
我想保持这个解决方案尽可能干净和最小。
采纳答案by YOU
datejscould parse following, you might want to try out.
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
Edit:Regex version
编辑:正则表达式版本
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(January 28, 2011 - 7:30PM EST
,,,,,,){
return MM[-1]+" "++", "++" - "+%12+":"++(+>12?"PM":"AM")+" "+
}
)
Result
结果
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(January 28, 2011 - 7:30PM EST
,,,,,,){
return MM[]+" "++", "++" - "+%12+":"++(+>12?"PM":"AM")+" "+
}
)
Edit2:I changed my timezone to EST and now I got following
Edit2:我将时区更改为 EST,现在我得到了关注
String(new Date(x))
return
返回
Fri Jan 28 2011 19:30:00 GMT-0500 (EST)
Basically
基本上
January 28, 2011 - 7:30PM EST
return
返回
var d = new Date("2014-04-07T13:58:10.104Z");
console.log(d.toString());
regex parts just converting above string to your required format.
正则表达式部分只是将上面的字符串转换为您需要的格式。
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);
}
回答by Rob Evans
The Date object handles 8601 as it's first parameter:
Date 对象处理 8601 作为它的第一个参数:
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();
回答by Ciantic
If you want to keep it simple, this should suffice:
如果你想保持简单,这应该足够了:
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));
noteparseFloat is must, parseInt doesn't always work. Map requires IE9 or later.
注意parseFloat 是必须的, parseInt 并不总是有效。地图需要 IE9 或更高版本。
Works for formats:
适用于格式:
- 2014-12-28 15:30:30
- 2014-12-28T15:30:30
- 2014-12-28
- 2014-12-28 15:30:30
- 2014-12-28T15:30:30
- 2014-12-28
Not valid for timezones, see other answers about those.
对时区无效,请参阅有关这些的其他答案。
回答by alex-arriaga
Maybe, you can use moment.js which in my opinion is the best JavaScript library for parsing, formatting and working with dates client-side. You could use something like:
也许,您可以使用 moment.js,在我看来,它是解析、格式化和处理客户端日期的最佳 JavaScript 库。你可以使用类似的东西:
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());
The advantage of using a library like moment.js is that your code will work perfectly even in legacy browsers like IE 8+.
使用像 moment.js 这样的库的优势在于,即使在 IE 8+ 等传统浏览器中,您的代码也能完美运行。
Here is the documenation about parsing methods: https://momentjs.com/docs/#/parsing/
这是有关解析方法的文档:https://momentjs.com/docs/#/parsing/
回答by Ryan V
According to MSDN, the JavaScript Date object does not provide any specific date formatting methods (as you may see with other programming languages). However, you can use a few of the Date
methods and formatting to accomplish your goal:
根据MSDN,JavaScript Date 对象不提供任何特定的日期格式化方法(正如您在其他编程语言中看到的那样)。但是,您可以使用一些Date
方法和格式来实现您的目标:
You could even take it one step further and override the Date.toString()
method:
您甚至可以更进一步并覆盖该Date.toString()
方法: