# Round Dates.Time variable

**URL:** <https://discourse.julialang.org/t/round-dates-time-variable/88306>\
**Category:** General Usage\
**Tags:** dates, time, rounding, timeperiod\
**Created:** [October 5, 2022, 4:49pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306 "2022-10-05T16:49:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [October 5, 2022, 4:49pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/1 "2022-10-05T16:49:41Z")

</div>

Hi,

I am trying to round two variables - say `X` and `Y` - of type `Dates.Time`. `X` has value `09:30:01.017`, `Y` has value `09:59:59.103` and I would like to round them to `09:30:00.000` and `10:00:00.000`. I have tried using `floor` and `ceil`, but I keep getting errors with `Dates.Time` variables. I would appreciate if someone could write down a simple example showcasing how to use them with similar DataTypes.

Thanks!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 5, 2022, 5:04pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/2 "2022-10-05T17:04:02Z")

</div>

Like this?

```julia
julia> t = now()
2022-10-05T13:03:49.422

julia> floor(t, Dates.Minute)
2022-10-05T13:03:00

```

---

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [October 5, 2022, 5:11pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/3 "2022-10-05T17:11:16Z")

</div>

Yes, but `t` is a `DateTime`, not a `Time`. My variables are of type `Time`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 5, 2022, 5:17pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/4 "2022-10-05T17:17:04Z")

</div>

Could be a missing method, maybe. If no one replies, file an issue

---

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [October 5, 2022, 5:39pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/5 "2022-10-05T17:39:10Z")

</div>

Will do. The below works, but it is a bit rough:

```julia
Dates.floor(X::Dates.Time, P::Dates.Period) = floor(DateTime("$(today())T$(X)", "yyyy-mm-ddTHH:MM:SS.s"), P) |> Time;
Dates.ceil(X::Dates.Time, P::Dates.Period) = ceil(DateTime("$(today())T$(X)", "yyyy-mm-ddTHH:MM:SS.s"), P) |> Time;

```

---

<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:** [October 5, 2022, 8:06pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/6 "2022-10-05T20:06:02Z")

</div>

You could also write user functions based on:

```julia
using Dates

t0 = Time("09:30:01.017")
t1 = Time(Nanosecond(round(Int, t0.instant/Nanosecond(1e9))*1e9))
09:30:01

t3 = Time("09:59:59.503")
t4 = Time(Nanosecond(round(Int, t3.instant/Nanosecond(1e9))*1e9))
10:00:00
```

---

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [October 6, 2022, 2:04pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/7 "2022-10-06T14:04:57Z")

</div>

This seems better, I am just not sure whether something similar is already implemented under Dates. I will open an issue on the Julia GitHub page and link this post.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 6, 2022, 5:46pm UTC](https://discourse.julialang.org/t/round-dates-time-variable/88306/8 "2022-10-06T17:46:43Z")

</div>

A proposal along the lines of @rafael.guerra  
The round function has methods for some types of DateTime.  
Perhaps there is something more specific and direct

```julia
round(dt::TimeType, p::Period, [r::RoundingMode]) -> TimeType    

  Return the Date or DateTime nearest to dt at resolution p. By    
  default (RoundNearestTiesUp), ties (e.g., rounding 9:30 to the   
  nearest hour) will be rounded up.

```

```julia
using Dates
t0=Time("09:30:01.534")
t3 = Time("09:59:59.503")
Time(Nanosecond(round(t0.instant, Dates.Second)))
Time(Nanosecond(round(t3.instant, Dates.Second)))

```
