# How to convert a string to ISO-standard datetime?

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-string-to-iso-standard-datetime/90261>\
**Category:** General Usage\
**Tags:** strings, dates\
**Created:** [November 14, 2022, 9:42pm UTC](https://discourse.julialang.org/t/how-to-convert-a-string-to-iso-standard-datetime/90261 "2022-11-14T21:42:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [November 14, 2022, 9:42pm UTC](https://discourse.julialang.org/t/how-to-convert-a-string-to-iso-standard-datetime/90261/1 "2022-11-14T21:42:07Z")

</div>

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](https://support.travelport.com/webhelp/uapi/Content/Getting_Started/Design_Considerations/Date_and_Time_Formats.htm) for date and times. To attempt a conversion from such a string to date and time, I did the following:

```julia
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](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates-and-Time-Types) 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.

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [November 14, 2022, 10:12pm UTC](https://discourse.julialang.org/t/how-to-convert-a-string-to-iso-standard-datetime/90261/2 "2022-11-14T22:12:31Z")

</div>

I found a way to make it work thanks for this question/answer duo from [StackOverflow](https://stackoverflow.com/questions/65309393/convert-iso8601-date-with-timezone-to-datetime-in-julia).

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 14, 2022, 10:33pm UTC](https://discourse.julialang.org/t/how-to-convert-a-string-to-iso-standard-datetime/90261/3 "2022-11-14T22:33:23Z")

</div>

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

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

```
