在 Excel 中解析 ISO8601 日期/时间(包括时区)
我需要将 Excel/VBA 中包含的时区(来自外部源)的 ISO8601 日期/时间格式解析为正常的 Excel 日期。据我所知,Excel XP(我们正在使用的)没有内置的例程,所以我想我正在寻找一个自定义的 VBA 函数来进行解析。
ISO8601 日期时间看起来像以下之一:
2011-01-01
2011-01-01T12:00:00Z
2011-01-01T12:00:00+05:00
2011-01-01T12:00:00-05:00
2011-01-01T12:00:00.05381+05:00
I need to parse an ISO8601 date/time format with an included timezone (from an external source) in Excel/VBA, to a normal Excel Date. As far as I can tell, Excel XP (which is what we're using) doesn't have a routine for that built-in, so I guess I'm looking at a custom VBA function for the parsing.
ISO8601 datetimes look like one of these:
2011-01-01
2011-01-01T12:00:00Z
2011-01-01T12:00:00+05:00
2011-01-01T12:00:00-05:00
2011-01-01T12:00:00.05381+05:00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有一种(相当)简单的方法可以使用公式而不是宏来解析不带时区的 ISO 时间戳。这并不完全是原始发帖者所问的问题,但我在尝试解析 Excel 中的 ISO 时间戳时发现了这个问题,并发现 这个解决方案很有用,所以我想我会在这里分享它。
以下公式将解析 ISO 时间戳,同样不包含时区:
这将生成浮点格式的日期,然后您可以使用普通 Excel 格式将其格式化为日期。
There is a (reasonably) simple way to parse an ISO timestamp WITHOUT the time zone using formulas instead of macros. This is not exactly what the original poster has asked, but I found this question when trying to parse ISO timestamps in Excel and found this solution useful, so I thought I would share it here.
The following formula will parse an ISO timestamp, again WITHOUT the time zone:
This will produce the date in floating point format, which you can then format as a date using normal Excel formats.
很多谷歌搜索都没有找到任何东西,所以我写了自己的例程。将其发布在这里以供将来参考:
A lot of Googling didn't turn up anything so I write my own routine. Posting it here for future reference:
我知道它不像 VB 模块那么优雅
但如果有人正在寻找一个快速公式,该公式也考虑“+”后的时区,那么这可能就是它。
显然,将更改为
(
考虑时区的本地时间),
如果您也有毫秒或者+后有更长的时间,您可以修改不同修剪部分的长度。
如果您想忽略时区,请使用 sigpwned 公式。
I know its not as elegant as VB module
but if someone is looking for a quick formula which consider time zone after '+' as well then this could be it.
will change
to
(local time considering timezone)
obviously, u can modify the length of different trimming sections, if u got milliseconds as well or if you got longer time after +.
use
sigpwned
formula if you want to ignore timezone.我本想将此作为评论发布,但我没有足够的代表 - 抱歉!这对我来说非常有用 - 感谢 rix0rrr,但我注意到 UTCToLocalTime 函数在最后构建日期时需要考虑区域设置。这是我在英国使用的版本 - 请注意 wDay 和 wMonth 的顺序相反:
I would have posted this as a comment, but I don't have enough rep - sorry!. This was really useful for me - thanks rix0rrr, but I noticed that the UTCToLocalTime function needs to take account of regional settings when constructing the date at the end. Here's the version I use in the UK - note that the order of wDay and wMonth are reversed:
您可以在不使用 VB for Applications 的情况下执行此操作:
例如解析以下内容:
do:
For
do:
For
do:
但上方的日期格式应由 Excel 自动解析。
然后您将获得一个 Excel 日期/时间值,您可以将其格式化为日期和时间。
有关详细信息和示例文件: http:// blog.hani-ibrahim.de/iso-8601-parsing-in-excel-and-calc.html
You can do this w/o VB for Applications:
E.g. to parse the following:
do:
For
do:
For
do:
but the upper date format should Excel parse automatically.
Then you get a Excel date/time value, which you can format to date and time.
For detailed information and sample files: http://blog.hani-ibrahim.de/iso-8601-parsing-in-excel-and-calc.html
我的日期格式为 20130221T133551Z (YYYYMMDD'T'HHMMSS'Z'),所以我创建了这个变体:(
时区转换未经测试,并且在意外输入的情况下没有错误处理)
My dates are on the form 20130221T133551Z (YYYYMMDD'T'HHMMSS'Z') so I created this variant:
(the timezone-conversion is not tested, and there is no error-handling in case of unexpected input)
rix0rrr 的答案 a> 很棒,但它不支持不带冒号或仅包含小时的时区偏移。我稍微增强了功能以添加对这些格式的支持:
The answer by rix0rrr is great, but it does not support time zone offsets without colon or with only hours. I slightly enhanced the function to add support for these formats:
将任何时区转换为 UTC 的完整公式,输入不包括秒:
=DATEVALUE(MID(D3,1,10))+TIMEVALUE(MID(D3,12,5))+(IF(MID( D3,17,1)="+",-1,1)*IFERROR(TIMEVALUE(MID(D3,18,5)),0))
支持:
2022-03-30T08:19 -01:00
2022-03-30T12:49+03:30
2022-03-30T12:19+03:00
2022-03 -30T09:19Z
2022-03-30T09:19
当输入包含秒时:
=DATEVALUE(MID(D3,1,10))+TIMEVALUE(MID(D3,12) ,8))+(IF(MID(D3,20,1)="+",-1,1)*IFERROR(TIMEVALUE(MID(D3,21,5)),0))
支持:
2022-03-30T08:19:14-01:00
2022-03-30T12:49:14+03:30
2022-03-30T12:19 :14+03:00
2022-03-30T09:19:14Z
2022-03-30T09:19:14
A complete formula for converting any time zone to UTC, input does not include seconds:
=DATEVALUE(MID(D3,1,10))+TIMEVALUE(MID(D3,12,5))+(IF(MID(D3,17,1)="+",-1,1)*IFERROR(TIMEVALUE(MID(D3,18,5)),0))
Supports:
2022-03-30T08:19-01:00
2022-03-30T12:49+03:30
2022-03-30T12:19+03:00
2022-03-30T09:19Z
2022-03-30T09:19
When input include seconds:
=DATEVALUE(MID(D3,1,10))+TIMEVALUE(MID(D3,12,8))+(IF(MID(D3,20,1)="+",-1,1)*IFERROR(TIMEVALUE(MID(D3,21,5)),0))
Supports:
2022-03-30T08:19:14-01:00
2022-03-30T12:49:14+03:30
2022-03-30T12:19:14+03:00
2022-03-30T09:19:14Z
2022-03-30T09:19:14
如果您只需将某些(固定)格式转换为 UTC 就足够了,您可以编写一个简单的 VBA 函数或公式。
下面的函数/公式适用于这些格式(无论如何都会省略毫秒):
VBA 函数
更长,为了更好的可读性:
...或“oneliner”:
公式
[@ISO]
是包含 ISO8601 格式的当地时间日期/时间的单元格(在表格内)。两者都会生成新的日期/时间类型值。您可以根据您的需要随意调整功能(特定日期/时间格式)。
If it's sufficient for you to convert just a certain (fixed) formats to UTC, you can write a simple VBA function or formula.
The function/formula below will work for these formats (milliseconds will be omitted anyway):
VBA function
Longer, for better readibility:
... or a "oneliner":
Formula
[@ISO]
is the cell (within a table) containing the date/time in local time in ISO8601 format.Both will generate new date/time type value. Feel free to adjust functions accordingly to your needs (specific date/time format).
我还没有验证上述自定义 VBA 函数,但有时函数会受到限制,并且不允许......就此而言,上面的“仅限 Excel 公式”解决方案似乎都不完整/正确。
因此,如果日期位于单元格
A1
中,则公式将为:...更详细的细分:
I've not verified the above custom VBA functions, but there are times where functions are restricted, and not permitted... and with respect to that, none of the 'Excel Formula Only' solutions above seemed to be either complete / correct.
Therefore, if the date is in cell
A1
, the formula would be:... for a more detailed breakdown: