# How convert a string with a date and time info with submilisecond resolution to DateTime or perhaps just Time?

**URL:** <https://discourse.julialang.org/t/how-convert-a-string-with-a-date-and-time-info-with-submilisecond-resolution-to-datetime-or-perhaps-just-time/32213>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 12, 2019, 10:56pm UTC](https://discourse.julialang.org/t/how-convert-a-string-with-a-date-and-time-info-with-submilisecond-resolution-to-datetime-or-perhaps-just-time/32213 "2019-12-12T22:56:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [December 12, 2019, 10:56pm UTC](https://discourse.julialang.org/t/how-convert-a-string-with-a-date-and-time-info-with-submilisecond-resolution-to-datetime-or-perhaps-just-time/32213/1 "2019-12-12T22:56:32Z")

</div>

What is the simplest way to convert the `2019-12-06 12:01:40.300199` string into a `DateTime` or perhaps `Time` type of a variable?

The trouble is obviously that the `DateTime` only considers integer milliseconds as the time resolution and the following

```julia
julia> df = DateFormat("Y-m-d H:M:S.s")
dateformat"Y-m-d H:M:S.s"

```

```julia
julia> DateTime("2019-12-06 12:01:40.300199",df)
ERROR: InexactError: convert(Dates.Decimal3, 300199)
Stacktrace:
 [1] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:153 [inlined]
 [2] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:41 [inlined]
 [3] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:64 [inlined]
 [4] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("Y-m-d H: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'}}}, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:40
 [5] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:150 [inlined]
 [6] tryparsenext_internal at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:127 [inlined]
 [7] parse(::Type{DateTime}, ::String, ::DateFormat{Symbol("Y-m-d H: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'}}}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:282
 [8] DateTime(::String, ::DateFormat{Symbol("Y-m-d H: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'}}}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:482
 [9] top-level scope at none:0

```

then does not work, while

```julia
julia> DateTime("2019-12-06 12:01:40.301",df)
2019-12-06T12:01:40.301

```

is OK.

As a matter of fact, I do not need to keep the `Date` part. I am only interested in the `Time` part. I can split the string into the two parts

```julia
julia> split("2019-12-06 12:01:40.300199")
2-element Array{SubString{String},1}:
 "2019-12-06"     
 "12:01:40.300199"

```

but what shall I do next? Just converting the string into `Time` as in

```julia
julia> Time("12:01:40.300199")
ERROR: InexactError: convert(Dates.Decimal3, 300199)
Stacktrace:
 [1] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:153 [inlined]
 [2] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:41 [inlined]
 [3] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:64 [inlined]
 [4] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("HH:MM:SS.s"),Tuple{Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'},Dates.Delim{Char,1},Dates.DatePart{'s'}}}, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:40
 [5] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:150 [inlined]
 [6] tryparsenext_internal at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:127 [inlined]
 [7] parse at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:282 [inlined]
 [8] Time at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:524 [inlined] (repeats 2 times)
 [9] top-level scope at none:0

```

does not work. No surprise, the [documentation](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.Time-Tuple%7BDateTime%7D) clearly says that microseconds and nanoseconds are set to zero. I simply cannot find in the documentation anything equivalent to `DateFormat`. I seem to be unable to decipher the rest of the documentation for `Time`. Any advice on how to proceed systematically without parsing the string manually and fitting into the n-tuple for construction as described in the [documentation](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.Time-NTuple%7B5,Int64%7D)?

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [December 13, 2019, 6:16pm UTC](https://discourse.julialang.org/t/how-convert-a-string-with-a-date-and-time-info-with-submilisecond-resolution-to-datetime-or-perhaps-just-time/32213/2 "2019-12-13T18:16:18Z")

</div>

I think you have two choices (not mutually exclusive): do the manual parsing, (e.g. `ms = parse(Int,s[21:23])`, `μs = parse(Int,s[24:26])` ) and/or do a PR to add a new Code `μ`, which Matches `199` with microseconds.
