diff --git a/stdlib/Dates/src/types.jl b/stdlib/Dates/src/types.jl index c0202bd9171b3a..2a011a9c7ebe1d 100644 --- a/stdlib/Dates/src/types.jl +++ b/stdlib/Dates/src/types.jl @@ -240,7 +240,7 @@ function DateTime(y::Year, m::Month=Month(1), d::Day=Day(1), h::Hour=Hour(0), mi::Minute=Minute(0), s::Second=Second(0), ms::Millisecond=Millisecond(0)) return DateTime(value(y), value(m), value(d), - value(h), value(mi), value(s), value(ms)) + value(h), value(mi), value(s), value(ms)) end Date(y::Year, m::Month=Month(1), d::Day=Day(1)) = Date(value(y), value(m), value(d)) @@ -310,6 +310,26 @@ function Time(period::TimePeriod, periods::TimePeriod...) return Time(h, mi, s, ms, us, ns) end +# Convenience constructor for DateTime from Date and Time +""" + DateTime(d::Date, t::Time) + +Construct a `DateTime` type by `Date` and `Time`. + +```jldoctest +julia> d = Date(2018, 1, 1) +2018-01-01 + +julia> t = Time(8, 15, 42) +08:15:42 + +julia> DateTime(d, t) +2018-01-01T08:15:42 +``` +""" +DateTime(d::Date, t::Time) = + DateTime(UTM(value(d) * 86400000 + value(t) / 1000000)) + # Fallback constructors DateTime(y, m=1, d=1, h=0, mi=0, s=0, ms=0) = DateTime(Int64(y), Int64(m), Int64(d), Int64(h), Int64(mi), Int64(s), Int64(ms)) Date(y, m=1, d=1) = Date(Int64(y), Int64(m), Int64(d))