AJW
AJW

Reputation: 5863

datetime string (from python) to matlab

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

Upvotes: 0

Views: 118

Answers (2)

Nathan
Nathan

Reputation: 391

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

Upvotes: 2

FObersteiner
FObersteiner

Reputation: 25544

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).

Upvotes: 2

Related Questions