# Build a DateTime from a Date and a Time

**URL:** https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437
**Category:** General Usage
**Created:** [March 2, 2018, 2:32am UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437 "2018-03-02T02:32:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [March 2, 2018, 2:32am UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437/1 "2018-03-02T02:32:07Z")

</div>

Hi all,

I’d like to extend the `DateTime` constructor to accept `DateTime(x::Date, y::Time)`. Can anyone think of anything more sensible than the following?

```julia
DateTime(x::Date, y::Time) = DateTime(x) + Millisecond(Int(round(Dates.value(y) / 1000000)))

```

I just convert the nanosecond from epoch to milliseconds from epoch and then add it to a `DateTime` that starts at the beginning of the day.

I’m not 100% convinced by `round`. Maybe `floor` or `ceil` would be more appropriate?

Would this be a sensible method to include in `Dates`? I would have thought it quite a common operation…

Cheers,

Colin

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [March 2, 2018, 5:31am UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437/2 "2018-03-02T05:31:02Z")

</div>

```julia
using Dates
d = Date("2001-01-01")
t = Time("20:05")
dt = d + t

```

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [March 2, 2018, 9:18am UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437/3 "2018-03-02T09:18:05Z")

</div>

Ah! Thank you! I thought it was strange that there wasn’t a constructor that took a `Date` and `Time` input. This explains why.

I must admit, I still feel like a `DateTime` constructor would be more natural than adding a `Date` and `Time`. A `Date` does not intrinsically have a “time” component. For example, `today() + Hour(1)` is an error, so why should `today() + Time(now())` work? But I guess ultimately it is a judgment call, and I am by no means an expert at any of this.

Cheers and thanks,

Colin

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [March 2, 2018, 4:30pm UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437/4 "2018-03-02T16:30:04Z")

</div>

There are a few more constructors:

```julia
DateTime(2013, 7, 1, 12, 30, 59)
DateTime(Dates.Year(2013), Dates.Month(7), Dates.Day(1),
         Dates.Hour(12), Dates.Minute(23))

```

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [March 3, 2018, 10:22pm UTC](https://discourse.julialang.org/t/build-a-datetime-from-a-date-and-a-time/9437/5 "2018-03-03T22:22:07Z")

</div>

Yes those ones are all in the official docs so I’m familiar with them. But I don’t think the Date + Time is in there (yet).

Cheers,

Colin
