1-hour offset in time string conversion

20 ビュー (過去 30 日間)
Dennis
Dennis 2014 年 10 月 24 日
回答済み: Dennis 2014 年 11 月 3 日
Hi there, I'm having trouble in converting string to datenum values. Somehow, there will be an offset of one hour when converting back to datestr. So, here is my example:
>> timestr = '1986-03-13T11:59:58.000Z';
>> test = datenum(timestr,'yyyy-mm-ddTHH:MM:SS.000Z')
test =
7.2544e+05
>> datestr(test,'yyyy-mm-ddTHH:MM:SS')
ans =
1986-03-13T12:59:58
To me, this seems pretty weird. I might be missing something very simple, though. I'd be glad if any of you could help me! Thanks
Dennis
  1 件のコメント
Star Strider
Star Strider 2014 年 10 月 24 日
I suggest you report this as a bug, specifically because in R2014b I get an even weirder result from running your code:
1986-03-13T04:59:58
a -19 hour error!

サインインしてコメントする。

採用された回答

Peter Perkins
Peter Perkins 2014 年 10 月 31 日
Dennis, I'm guessing you're somewhere in Europe, and that Star Strider is somewhere in the western US. As in, UTC+1 and UTC-7.
Reason is, the Z in your string is being treated as "Zulu", i.e., UTC, and the datenum is converted to your local time zone. Z has never (far as I know) been documented as something you could put in a date format for datestr. Prior to R2013a, this threw an error.
I'm not sure what you need to do with a date string like this. You may simply want to ignore the Z and preserve the time stamp itself. If they all have a trailing 'Z', I recommend just stripping that off.
If you have access to R2014b, you might consider using the new date and time features. For example:
>> timestr = '1986-03-13T11:59:58.000Z'
timestr =
1986-03-13T11:59:58.000Z
>> datetime(timestr,'Format','yyyy-MM-dd''T''HH:mm:ss.SSSXXX','TimeZone','UTC')
ans =
1986-03-13T11:59:58.000Z
>> datetime(timestr,'Format','yyyy-MM-dd''T''HH:mm:ss.SSSXXX','TimeZone','Europe/Paris')
ans =
1986-03-13T12:59:58.000+01:00
>> datetime(timestr,'Format','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''')
ans =
1986-03-13T11:59:58.000Z
Hope this helps.

その他の回答 (1 件)

Dennis
Dennis 2014 年 11 月 3 日
I have not thought about this, stupid me... Anyways, the easiest fix for me will then be to simply omit the 'Z' from the format string! Thanks a lot!

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by