简体   繁体   English

如何解析包含时区的日期字符串?

[英]How to parse a date String containing a timezone?

I have date in String: 我在String中有日期:

let day = "12/07/1998 12:30 GMT+4"

How to convert it into a moment date? 如何将其转换为即时日期? I tried 我试过了

let d = moment(day, 'DD/MM/YYYY HH:mm')

But I don't know how to parse the timezone. 但是我不知道如何解析时区。 I could not find it in the documentation 我在文档中找不到它

Updated 2018/11/6: 更新于2018/11/6:

Newer versions of moment expect you to pass timezone format as one of these two options: 较新版本的moment希望您通过时区格式作为以下两个选项之一:

ZZ=> -07:00 -06:00 ... +06:00 +07:00 ZZ => -07:00 -06:00 ... +06:00 +07:00

Or 要么

Z => -0700 -0600 ... +0600 +0700 Z => -0700 -0600 ... +0600 +0700

Read More Here.. 在这里阅读更多..

So we need to repair our given GMT+4 format to match the input case we will use some regex and es6 templating to reformat our given date like this: DD/MM/YYYY hh:mm ZZ notice 'ZZ' at the end: 因此,我们需要修复给定的GMT + 4格式以匹配输入大小写,我们将使用一些正则表达式和es6模板来重新格式化给定的日期,如下所示: DD/MM/YYYY hh:mm ZZ在末尾注意'ZZ':

Demo: 演示:

 let date = "12/07/1998 12:30 GMT+4" let justDayAndTime = date.split(/(GMT(\\+|\\-)\\d+)/)[0].trim() let zone = date.match(/\\d+$/)[0] let mark = date.match(/(\\+|\\-)/)[0] let parsed = `${justDayAndTime} ${mark}${moment(zone,"k").format("HH:mm")}` let result = moment(parsed,"DD/MM/YYYY hh:mm ZZ") console.log(result.format('LLLL')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> 


Answer: 回答:

You can use the format method 您可以使用格式方法

 moment("12/07/1998 12:30 GMT+4").format('DD/MM/YYYY HH:mm') 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> 

For more Time Zone control: 有关更多时区控制:

https://momentjs.com/timezone/ https://momentjs.com/timezone/

Moment.js doesn't necessarily parse the timezone if the string isn't in either ISO 8601 or RFC 2822 formats. 如果字符串不是ISO 8601或RFC 2822格式,则Moment.js不一定会解析时区。 It expects the timezone to be in ±HHmm format, so GMT+4 is ignored. 它期望时区为±HHmm格式,因此GMT+4被忽略。

If you pass the string to the constructor with no format information, you'll get a warning and maybe it will be parsed correctly and maybe it wont. 如果将字符串传递给不带格式信息的构造函数,则会收到警告,并且可能会正确解析它,也可能不会。

If you can't fix the input format at the source, you can fix it at your end (provided you know the format), then provide "Z" in the parse tokens, eg: 如果您无法在源代码处修复输入格式,则可以在最后修复它(前提是您知道该格式),然后在解析标记中提供“ Z”,例如:

 /* Fix offset like GMT+4 to +0400 * If offset is 1 digit, pad to 0d00 -8 -> -0800 * If offset is 2 digits, pad to dd00 +10 -> +1000 * If offset is 3 digits, padd to 0ddd +530 -> +0530 * If offset is 4 digits, leave as is +1030 -> +1030 */ function fixOffset(s) { var tz = s.match(/(GMT)([+-])(\\d+)$/); // If not required format, do nothing if (tz.length != 4) return; var h = tz[3]; var offset = h.length == 1? '0' + h + '00' : h.length == 2? h + '00' : h.length == 3? '0' + h : h; return s.replace(/GMT[+-]\\d+$/, tz[2] + offset); } var s = "12/07/1998 12:30 GMT+4"; s = fixOffset(s); console.log('Fixed format: ' + s); console.log( moment(s,'DD/MM/YYYY HH:mm Z').format('LLLL') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> 

The best way to parse Timezones string, is to use moment timezone. 解析Timezones字符串的最佳方法是使用moment timezone。 You have to do like this moment.tz(day) 你必须像这个moment.tz(day)那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM