# Rounding DateTime

**URL:** https://discourse.julialang.org/t/rounding-datetime/52603
**Category:** New to Julia
**Created:** [December 30, 2020, 6:34am UTC](https://discourse.julialang.org/t/rounding-datetime/52603 "2020-12-30T06:34:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![chrisk477](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisk477/32/20653_2.png) [@chrisk477](https://discourse.julialang.org/u/chrisk477)
#### Post date: [December 30, 2020, 6:34am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/1 "2020-12-30T06:34:51Z")

</div>

Hello,

sorry for the basic question.

I want to round the time values of a time series to various periods, let’s say a 10-second-period, so that for example 10:23:47 becomes 10:23:40

I wanted to do this with the following code:

julia\> dt = DateTime(2020, 03, 23, 07, 29, 37, 869)  
2020-03-23T07:29:37.869

julia\> dtval = Dates.value(dt)  
63720631777869

julia\> dtval = dtval - (dtval % 10000)  
63720631770000

But I don’t know how to construct a DateTime object from the Int64 representation. Can anyone help?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 30, 2020, 6:44am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/2 "2020-12-30T06:44:27Z")

</div>

The docstring for `round` contains some clues that may be useful

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

  For convenience, p may be a type instead of a value: round(dt, Dates.Hour) is a shortcut for round(dt,
  Dates.Hour(1)).

  julia> round(Date(1985, 8, 16), Dates.Month)
  1985-08-01
  
  julia> round(DateTime(2013, 2, 13, 0, 31, 20), Dates.Minute(15))
  2013-02-13T00:30:00
  
  julia> round(DateTime(2016, 8, 6, 12, 0, 0), Dates.Day)
  2016-08-07T00:00:00

  Valid rounding modes for round(::TimeType, ::Period, ::RoundingMode) are RoundNearestTiesUp (default), RoundDown
  (floor), and RoundUp (ceil).

  ────────────────────────────────────────────────────────────────
  round(x::Period, precision::T, [r::RoundingMode]) where T <: Union{TimePeriod, Week, Day} -> T

  Round x to the nearest multiple of precision. If x and precision are different subtypes of Period, the return value
  will have the same type as precision. By default (RoundNearestTiesUp), ties (e.g., rounding 90 minutes to the
  nearest hour) will be rounded up.

  For convenience, precision may be a type instead of a value: round(x, Dates.Hour) is a shortcut for round(x,
  Dates.Hour(1)).

  julia> round(Dates.Day(16), Dates.Week)
  2 weeks
  
  julia> round(Dates.Minute(44), Dates.Minute(15))
  45 minutes
  
  julia> round(Dates.Hour(36), Dates.Day)
  2 days

```

---

<div class="post-metadata">

### Author: ![chrisk477](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisk477/32/20653_2.png) [@chrisk477](https://discourse.julialang.org/u/chrisk477)
#### Post date: [December 30, 2020, 7:01am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/3 "2020-12-30T07:01:11Z")

</div>

Wow, that’s much easier than I thought – I didn’t know the round() function yet.  
Thanks!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 30, 2020, 7:47am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/4 "2020-12-30T07:47:15Z")

</div>

The REPL help mode has some nice search functionalities, e.g., asking for help on a string searches all docstrings for that string

```julia
help?> "round"
Base.cld
Base.round

```

You can also call the function `apropos` directly.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 30, 2020, 9:33am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/5 "2020-12-30T09:33:27Z")

</div>

I didn’t know this, is this new? And how is it different from `apropos("round")`, which returns lots more (in this case mostly unhelpful, I’d say…) hits?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 30, 2020, 9:40am UTC](https://discourse.julialang.org/t/rounding-datetime/52603/6 "2020-12-30T09:40:22Z")

</div>

They are equivalent, I only included the first two hits when I illustrated the usage 🙂
