如何将Zulu时区域字符串转换为特定的DateTime对象?
我有一个带有祖鲁时区的字符串格式的日期。我试图用正则表达式去掉“Z”字符,但我想有一种更有效的方法。
输入:
|index | date | municipality
|------| --------------------|--------------
| 0 | 07.02.2021 1017Z | Algier
| 1 | 11.01.2019 1716Z | Abuja
| 2 | 23.02.2018 1002Z | Brüssel
| 3 | 19.07.2021 1459Z | Brüssel
| 4 | 26.11.2019 1049Z | Berlin
期望结果:
|index | date | municipality
|------| --------------------|--------------
| 0 | 2021-02-17 | Algier
| 1 | 2019-01-11 | Abuja
| 2 | 2018-02-23 | Bruxelles
| 3 | 2021-07-19 | Bruxelles
| 4 | 2019-11-26 | Berlin
I have a date in string format with zulu time zone. I tried to get rid of the "Z" character with regular expression, but I guess there is a more efficient way.
input:
|index | date | municipality
|------| --------------------|--------------
| 0 | 07.02.2021 1017Z | Algier
| 1 | 11.01.2019 1716Z | Abuja
| 2 | 23.02.2018 1002Z | Brüssel
| 3 | 19.07.2021 1459Z | Brüssel
| 4 | 26.11.2019 1049Z | Berlin
desired outcome:
|index | date | municipality
|------| --------------------|--------------
| 0 | 2021-02-17 | Algier
| 1 | 2019-01-11 | Abuja
| 2 | 2018-02-23 | Bruxelles
| 3 | 2021-07-19 | Bruxelles
| 4 | 2019-11-26 | Berlin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要删除 Z 字符,而是正确解析它。 EX:
请注意,设置
format
关键字是可选的,但显式指定它有助于提高总体可靠性。如果您不想要的话,您也可以减少时间:
...或格式化为字符串:
Instead of getting rid of the Z character, parse it correctly. EX:
Note that setting the
format
keyword is optional, but it helps for general reliability to specify it explicitly.You can also floor the hours if you don't want them:
...or format to string:
我想这会很好用。此外,您可以在转换期间对日期进行一些计算。
This will works fine, I think. Additionally you can make some calculations with dates during transformation.
Alexei的方法也是一个很棒的解决方案,我们可以将其代码切换为函数并使用IT示例:
Alexei's method is a great solution also, we can switch its code into a function and use it example :