# Parsing datetime string

**URL:** https://discourse.julialang.org/t/parsing-datetime-string/65179
**Category:** General Usage
**Tags:** question, dates, parsing
**Created:** [July 23, 2021, 1:45pm UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179 "2021-07-23T13:45:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 23, 2021, 1:45pm UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/1 "2021-07-23T13:45:08Z")

</div>

Hi all 🙂

I want to parse the following datetime string but can’t find the right format.

```julia
julia> using Dates

julia> format = dateformat"y-m-dTH:M:S.s"
dateformat"y-m-dTH:M:S.s"

julia> dt_str = "2021-05-26T21:00:19.1816626Z"
"2021-05-26T21:00:19.1816626Z"

julia> DateTime(dt_str, format)
ERROR: InexactError: convert(Dates.Decimal3, 1816626)
Stacktrace:
 [1] tryparsenext
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:153 [inlined]
 [2] tryparsenext
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:41 [inlined]
 [3] macro expansion
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:64 [inlined]
 [4] tryparsenext_core(str::String, pos::Int64, len::Int64, df::DateFormat{Symbol("y-m-dTH:M:S.s"), Tuple{Dates.DatePart{'y'}, Dates.Delim{Char, 1}, Dates.DatePart{'m'}, Dates.Delim{Char, 1}, Dates.DatePart{'d'}, Dates.Delim{Char, 1}, Dates.DatePart{'H'}, Dates.Delim{Char, 1}, Dates.DatePart{'M'}, Dates.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}}, raise::Bool)
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:38
 [5] macro expansion
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:150 [inlined]
 [6] tryparsenext_internal
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:125 [inlined]
 [7] parse(::Type{DateTime}, str::String, df::DateFormat{Symbol("y-m-dTH:M:S.s"), Tuple{Dates.DatePart{'y'}, Dates.Delim{Char, 1}, Dates.DatePart{'m'}, Dates.Delim{Char, 1}, Dates.DatePart{'d'}, Dates.Delim{Char, 1}, Dates.DatePart{'H'}, Dates.Delim{Char, 1}, Dates.DatePart{'M'}, Dates.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}})
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:282
 [8] DateTime(dt::String, df::DateFormat{Symbol("y-m-dTH:M:S.s"), Tuple{Dates.DatePart{'y'}, Dates.Delim{Char, 1}, Dates.DatePart{'m'}, Dates.Delim{Char, 1}, Dates.DatePart{'d'}, Dates.Delim{Char, 1}, Dates.DatePart{'H'}, Dates.Delim{Char, 1}, Dates.DatePart{'M'}, Dates.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}})
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:483
 [9] top-level scope
   @ REPL[4]:1

```

I know that Julias `DateTime` isn’t that accurate. But it’s good enough for me.

---

<div class="post-metadata">

### Author: ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)
#### Post date: [July 23, 2021, 3:46pm UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/2 "2021-07-23T15:46:42Z")

</div>

It is complaining about the millisecond part. The “s” only matches three digit milliseconds. So both “20” and “200” get parsed as 200 milliseconds. And providing any more than three digits throws the same error you’re seeing.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 26, 2021, 8:11am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/3 "2021-07-26T08:11:09Z")

</div>

Is there a way to parse all `1816626Z` seven digits and cut it off?

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 26, 2021, 8:20am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/4 "2021-07-26T08:20:00Z")

</div>

You can just cutoff extra chars. Since this is ISO format, number of used characters is always the same.

```julia
julia> using Dates

julia> format = dateformat"y-m-dTH:M:S"
dateformat"y-m-dTH:M:S"

julia> dt_str = "2021-05-26T21:00:19.1816626Z"
"2021-05-26T21:00:19.1816626Z"

julia> DateTime(dt_str[1:19], format)
2021-05-26T21:00:19

```

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 26, 2021, 8:28am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/5 "2021-07-26T08:28:08Z")

</div>

I don’t think that works for me. I want the most precision as possible. I see that I could use a longer substring, but what if the source string ends with `.0`. Does anyone thinks that such cases should be handled by `Dates`?

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [July 26, 2021, 8:33am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/6 "2021-07-26T08:33:10Z")

</div>

This one could work for me… But I would really prefer it if `Dates` could handle that.

```julia
DateTime(dt_str[1:min(end-1, 23)], format)

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 26, 2021, 9:31am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/8 "2021-07-26T09:31:30Z")

</div>

`DateTime` only supports up to millisecond resolution (per [the docs](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.DateTime)), so I don’t think you can force higher precision than that by using `DateTime`.

`DateTime` is only a thin wrapper around `UTInstant{Millisecond}`, so if you require higher precision, you should be able to follow what it does if you want to implement your own:

[https://github.com/JuliaLang/julia/blob/f7f46af8ff39a1b4c7000651c680058e9c0639f5/stdlib/Dates/src/types.jl#L142-L151](https://github.com/JuliaLang/julia/blob/f7f46af8ff39a1b4c7000651c680058e9c0639f5/stdlib/Dates/src/types.jl#L142-L151)

---

<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: [July 26, 2021, 10:09am UTC](https://discourse.julialang.org/t/parsing-datetime-string/65179/9 "2021-07-26T10:09:12Z")

</div>

> [@Impressium](#):
>
> I want the most precision as possible

The [TimesDates](https://jeffreysarnoff.github.io/TimesDates.jl/) package announced [here](https://discourse.julialang.org/t/ann-timesdates-jl-times-zones-with-nanoseconds/10819) handles nanoseconds:

```julia
using TimesDates
dt_str = "2021-05-26T21:00:19.5816626Z"
tnano = TimeDate(dt_str[1:26])
2021-05-26T21:00:19.581662

julia> typeof(tnano)
TimeDate

```
