# Rounding DateTime string

**URL:** https://discourse.julialang.org/t/rounding-datetime-string/102434
**Category:** General Usage
**Created:** [August 3, 2023, 7:19am UTC](https://discourse.julialang.org/t/rounding-datetime-string/102434 "2023-08-03T07:19:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)
#### Post date: [August 3, 2023, 7:19am UTC](https://discourse.julialang.org/t/rounding-datetime-string/102434/1 "2023-08-03T07:19:22Z")

</div>

As discussed here

[https://discourse.julialang.org/t/parsing-datetime-string/](https://discourse.julialang.org/t/parsing-datetime-string/)

it’s quite a hassle to convert a datetime string to a `DateTime` object when the string contains more digits than `DateTime` can accommodate.

Is there already a simple solution?

If not . . .

There is an analogous situation when you want an approximate `Int` value from a string that represents a floating point number:

```julia
f = parse(Float64, "3.14")
i = round(Int, f)

```

So, if there were a “floating-point DateTime” type, we would solve the problem by

```julia
f = parse(FloatDateTime, "2023-07-01T12:34:56.123456789")
i = round(DateTime, f)

```

where the millisecond part of `FloatDateTime` would be represented internally by a `Float64` . . . Likely this would be overkill as you would then have to implement all operations on `FloatDateTime` . . .

Barring such a drastic solution, I would just split the string at the decimal point, convert the left part to a `DateTime`, approximate the right part to a 3-digit precision, perhaps with a warning message when the milliseconds is an approximation, and add the latter milliseconds to the former `DateTime`; or perhaps I would snatch the parser code from the `Dates` package and modify the milliseconds part to do the approximation; and always use such a personal parser every time I parse a `DateTime` . . .

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [August 3, 2023, 8:02am UTC](https://discourse.julialang.org/t/rounding-datetime-string/102434/2 "2023-08-03T08:02:09Z")

</div>

While not ideal, at least it “solves” the problem:

```julia
julia> str = "2023-07-01T12:34:56.123456789"
"2023-07-01T12:34:56.123456789"

julia> str2 = only(match(r"(.*\.\d{3})", str)) # discard anything above 
"2023-07-01T12:34:56.123"

julia> DateTime(str2)
2023-07-01T12:34:56.123

```

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [August 3, 2023, 8:10am UTC](https://discourse.julialang.org/t/rounding-datetime-string/102434/3 "2023-08-03T08:10:28Z")

</div>

This is better in the sense that it takes care of any rounding errors we ignored with the previous solution:

```julia
julia> dt, p = match(r"(.*)\.(\d*)", str) # split it to datetime and the fraction of the seconds
RegexMatch("2023-07-01T12:34:56.123456789", 1="2023-07-01T12:34:56", 2="123456789")

julia> ms = 1000parse(Float64, "0."*p)
123.456789

julia> DateTime(dt) + Millisecond(round(Int, ms)) # DateTime's accuracy goes only to milliseconds
2023-07-01T12:34:56.123

```

---

<div class="post-metadata">

### Author: ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)
#### Post date: [August 7, 2023, 5:28am UTC](https://discourse.julialang.org/t/rounding-datetime-string/102434/4 "2023-08-07T05:28:43Z")

</div>

Thanks! That’s indeed nice. I guess I’ll put that (plus error handling) into my personal “kitchen sink” module and use it everywhere I need to parse DateTime strings.

Here is a bit of simplification:

```julia
(dt, p) = split(str, ".")

```

According to the ISO standard (see the reference at the bottom of this posting), the decimal point is the only possibility that a DateTime string includes a period character, although your regex is a bit more robust in detecting error when a non-ISO-conforming string is given.

> **[ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)**
>
> 2023‐08‐07T04:44:51+00:00 UTC+00:00 \[refresh\]
> ISO 8601 is an international standard covering the worldwide exchange and communication of date and time-related data. It is maintained by the International Organization for Standardization (ISO) and was first published in 1988, with updates in 1991, 2000, 2004, and 2019, and an amendment in 2022. The standard provides a well-defined, unambiguous method of representing calendar dates and times in worldwide communications, especially to avoid misinter...
