检查ISO 8601字符串是否在UTC中
我想要一个打字稿功能,该功能检查ISO 8601 DateTime字符串是否在UTC中。因此,输出应该是:
isUTC('2020-01-01T00:00:00Z') // true
isUTC('2020-01-01T00:00:00+00') // true
isUTC('2020-01-01T00:00:00+0000) // true
isUTC('2020-01-01T00:00:00+00:00') // true
isUTC('2020-01-01T00:00:00+01) // false
isUTC('2020-01-01T00:00:00+0100) // false
isUTC('2020-01-01T00:00:00+01:00') // false
如果可能的话,我想避免正则表达式并使用现有的解析器或分解器。理想情况下,将有一个分解ISO 8601字符串的库,因此我可以检查iso8601decomposer(value).TimeZoneOffSet === 0
。类似的内容是否存在于Typescript/JavaScript中?
我发现一些答案检查value ===新date(date.parse(value))。toisostring()
,但这对于上面的第2行和第3行不起作用。
I want a TypeScript function that checks if an ISO 8601 datetime string is in UTC. So the output should be:
isUTC('2020-01-01T00:00:00Z') // true
isUTC('2020-01-01T00:00:00+00') // true
isUTC('2020-01-01T00:00:00+0000) // true
isUTC('2020-01-01T00:00:00+00:00') // true
isUTC('2020-01-01T00:00:00+01) // false
isUTC('2020-01-01T00:00:00+0100) // false
isUTC('2020-01-01T00:00:00+01:00') // false
If possible I would like to avoid regular expressions and use an existing parser or decomposer instead. Ideally there would be a library that decomposes ISO 8601 strings, so that I can check Iso8601Decomposer(value).timezoneOffset === 0
. Does something like this exist in TypeScript/JavaScript?
I found some answers that were checking if value === new Date(Date.parse(value)).toISOString()
, but this does not work for lines 2 and 3 above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
luxon
库ISO日期字符串。
然后,可以在几分钟内获得UTC偏移,如果是0,我们在UTC(或GMT)中,
我将所有这些都包装在必需的
iSUTC()
函数中。您 can 也可以使用正则态度来执行此操作:
You can use the
luxon
library to parse ISO date strings.One can then get the UTC offset in minutes, if this is 0 we're in UTC (or GMT)
I've wrapped this all up in the required
isUTC()
function.You can do this with RegEx too: