在Javascript中解析ISO 8601日期
作者:互联网
需要有关将具有以下结构的ISO 8601日期转换为javascript的帮助/提示.
CCYY-MM-DDThh:mm:ssTZD
我想照这样的日期:
January 28, 2011 - 7:30PM EST
我想让这个解决方案尽可能干净简洁.
解决方法:
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
Edit2:我把我的时区改为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
标签:iso8601,javascript,datetime,formatting 来源: https://codeday.me/bug/20190915/1805334.html