How to convert a string to ISO-standard datetime?

I have a DataFrame with a column filled with type string for date and time. The strings are formatted as "2017-01-01 00:00:00+00:00", for instance. Searching online I found that this format is the ISO-standard for date and times. To attempt a conversion from such a string to date and time, I did the following:

using Dates

x = "2017-01-01 00:00:00+00:00"
date_format = DateFormat("yyyy-mm-ddTHH:MM:SS")

x = Date(x, date_format)

My intention was to transform x from a string to a timestamp. Ideally, I would later broadcast the Date function to a column of an entire dataframe, but so far it’s been to no avail. I checked the documentation and found the following:

When creating a format you can use any non-code characters as a separator. For example to generate the string “1996-01-15T00:00:00” you could use format : “yyyy-mm-ddTHH:MM:SS”. Note that if you need to use a code character as a literal you can use the escape character backslash. The string “1996y01m” can be produced with the format “yyyy\ymm\m”.

I’m fully aware that "yyyy-mm-dd HH:MM:SS+00:00" is not the same as "yyyy-mm-ddTHH:MM:SS", but I tried creating date_format with "yyyy-mm-dd HH:MM:SS+00:00", but it failed.

All and any help will be appreciated.

I found a way to make it work thanks for this question/answer duo from StackOverflow.

For a vector x for strings formatted "2017-01-01 00:00:00+00:00", do this

using Dates, TimeZones

date_format = DateFormat("y-m-d HH:MM:SSzzzz")
DateTime.(x. date_format)

Not only does this work, it also drops the timezone part, which I intended to do anyway. The original post in the link actually shows a way to split the string before conversion.

If it is just to remove the time zone, it may not be necessary to use that package:

using Dates
x = "2017-01-01 00:00:00+00:00"
DateTime(first(split(x,'+')), "yyyy-mm-dd HH:MM:SS")
1 Like