0

I have a datatime string (which comes from django/python) which looks like so:

datatime_str='2020-08-18 16:48:13.722422+00:00'

I then do, in Matlab 2018a:

fmt_dt='yyyy-MM-dd HH:mm:ss.SSSSSS+HH:mm';
datetime(datatime_str,'TimeZone','local','Format',fmt_dt);

and I get:

2020-08-18 00:00:13.722422+00:00

I am not sure what it is that I am doing wrong, but the result is obviously wrong :(

Any help would be great

1
  • Which timezone do you belong to? Commented Aug 19, 2020 at 11:28

2 Answers 2

2

Yes the +00:00 should be formatted as timezone, not hours, minute. However, you can set the display format as you would like, and this could be different from the input. The default Matlab datetime display format discards the fractional seconds (and also the timezone I think). For example:

fmt_dt_input='yyyy-MM-dd HH:mm:ss.SSSSSSxxxxx';
fmt_dt_show='yyyy-MM-dd HH:mm:ss.SSSSSS xxxxx';
datatime_str='2020-08-18 16:48:13.722422+00:00';

t =  datetime(datatime_str,'InputFormat',fmt_dt_input,'TimeZone','local','Format',fmt_dt_show)

Has as output: 2020-08-18 16:48:13.722422 +00:00

EDIT: btw this info on datetime can be found here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a bunch for this - I also noticed that I needed to use InputFormat not Fomat
2

your input string contains a UTC offset at the end, +00:00 which you parse as hours and minutes - that is why they are set to 00:00 in the result. Use e.g.

datetime('2020-08-18 16:48:13.722422+00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSSSSSZ', 'TimeZone', 'UTC')

instead (change the TimeZone parameter to whatever you need).

5 Comments

This probably does not display the same as the input, since the default Matlab datetime display discards the fractional seconds in the display and also the timezone
@Nathan: right, I sometimes don't care so much how things are displayed, as long as it works correctly internally ;-)
Haha I understand.
@MrFuppes this works but the millisecond info seems missing :( Am I doing something wrong? Maybe it is just the display but how do I look at the entire value incl milliseconds?
@AJW: I think this is because I didn't provide a format as Nathan did in his answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.