# Need to find datetime for a datetime plus a duration/period/timedelta

**URL:** https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267
**Category:** General Usage
**Tags:** question, dates
**Created:** [February 14, 2021, 6:05pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267 "2021-02-14T18:05:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bvilmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvilmann/32/19775_2.png) [@bvilmann](https://discourse.julialang.org/u/bvilmann)
#### Post date: [February 14, 2021, 6:05pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267/1 "2021-02-14T18:05:33Z")

</div>

Suppose I have a period, defined by `Time()` and a datetime, `DateTime()`.

My goal is to to get a new datetime:

```julia
using Dates
startTime = DateTime(2021,2,14,19,00)
duration = Time(4,30)

endTime = startTime + duration

```

Above code returns:

```julia
ERROR: MethodError: no method matching +(::DateTime, ::Time)

```

How do I achieve getting the datetime for a datetime plus the duration / period / timedelta?

If any other approach reach my goal, it is more than welcome!

I cannot seem to find any lead, so I hope you, the incredible hive mind of Julia, can help me.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 14, 2021, 6:10pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267/2 "2021-02-14T18:10:39Z")

</div>

Your duration is not a duration though, it is being interpreted as an actual time of day. If you want 4.5 hours added to `start` then you can do `start+Minute(270)` which adds 270 minutes (4.5 hours).

---

<div class="post-metadata">

### Author: ![bvilmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvilmann/32/19775_2.png) [@bvilmann](https://discourse.julialang.org/u/bvilmann)
#### Post date: [February 14, 2021, 6:25pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267/3 "2021-02-14T18:25:43Z")

</div>

Aahh, great - thanks a lot!

Is there a convenience function for handling the duration more intuitively instead of total number of minutes? Which is doing something like the following?

```julia
function duration(h::Int64=0,m::Int64=0,s::Int64=0,ms::Int64=0,mus::Int64=0,ns::Int64=0)
    return (Hour(h) + Minute(m) + Second(s) + Millisecond(ms) + Microsecond(mus) + Nanosecond(ns))
end

```

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 14, 2021, 8:11pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267/4 "2021-02-14T20:11:39Z")

</div>

Not that I know if but you can also just do `start+Hour(4)+Minute(30)`

---

<div class="post-metadata">

### Author: ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)
#### Post date: [February 16, 2021, 3:18pm UTC](https://discourse.julialang.org/t/need-to-find-datetime-for-a-datetime-plus-a-duration-period-timedelta/55267/5 "2021-02-16T15:18:41Z")

</div>

`Dates.CompoundPeriod` could work as well but if you have consistent `duration` types then use the original answer. It gives an answer much faster.

```julia
julia> @time Dates.CompoundPeriod(duration) + startTime
  0.000026 seconds (20 allocations: 496 bytes)
2021-02-14T23:30:00

julia> @time startTime + Hour(4) + Minute(30)
  0.000012 seconds (9 allocations: 288 bytes)
2021-02-14T23:30:00

julia> @time startTime + Minute(270)
  0.000004 seconds (1 allocation: 16 bytes)
2021-02-14T23:30:00

```
