# Convert Dates.DateTime to int64 since epoch

**URL:** https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802
**Category:** New to Julia
**Created:** [July 9, 2020, 8:29pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802 "2020-07-09T20:29:26Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 9, 2020, 8:29pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/1 "2020-07-09T20:29:26Z")

</div>

What’s the correct way of converting DateTime to number of milliseconds since epoch? The function `datetime2unix` returns float.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [July 9, 2020, 8:47pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/2 "2020-07-09T20:47:22Z")

</div>

```julia
julia> using Dates

julia> n=now()
2020-07-09T22:41:28.191

julia> u=Int(floor(datetime2unix(n)))
1594334488

julia> unix2datetime(u)
2020-07-09T22:41:28

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 9, 2020, 8:52pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/3 "2020-07-09T20:52:57Z")

</div>

```julia
u=floor(Int, datetime2unix(n))

```

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 9, 2020, 8:54pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/4 "2020-07-09T20:54:57Z")

</div>

First off, that doesn’t seem efficient, as internally DateTime is an Int64, so you’re taking an Int64, converting it to Float, rounding it, and converting it to a different Int64. Second, it’s not even clear to me that this is correct for all values, given the rounding. Certainly the rounding loses you the milliseconds.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 9, 2020, 8:59pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/5 "2020-07-09T20:59:24Z")

</div>

```julia
julia> now().instant.periods.value
63729997158785

```

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 9, 2020, 9:01pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/6 "2020-07-09T21:01:22Z")

</div>

That’s an int, but it’s not since epoch.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 9, 2020, 9:03pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/7 "2020-07-09T21:03:47Z")

</div>

> [@freeman](#):
>
> `datetime2unix`

> <https://github.com/JuliaLang/julia/blob/44fa15b1502a45eac76c9017af94332d4557b251/stdlib/Dates/src/conversions.jl#L58>

yeah because you need to subtract epoch

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 9, 2020, 9:07pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/8 "2020-07-09T21:07:00Z")

</div>

Damn that’s a good find, thank you.

So the only reason why `datetime2unix` returns float is because of that `/1000.0`. Crazy. Implementing my own `datetime2epoch` now which returns int milliseconds 😃 Thank you

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [July 9, 2020, 10:32pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/9 "2020-07-09T22:32:02Z")

</div>

Freeman. Please do not keep all of us in **suspense**.

Please show us your source code for `datetime2epoch` function

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 9, 2020, 10:44pm UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/10 "2020-07-09T22:44:02Z")

</div>

Hahah well the point was just that if you don’t demand that the result to be in seconds and you’re ok with getting the result in milliseconds (or even better, nanoseconds[1]), then you don’t need the `/1000` bit, or the floats or the roundings.

So now I’m using:

```julia
datetime2epoch(x::DateTime) = (Dates.value(x) - Dates.UNIXEPOCH)*1_000_000

```

This returns an int with the number of nanoseconds since epoch.

[1] I say that nanoseconds is even better because it’s the internal representation of Python’s `pd.Timestamp`. It’s some little interop you gain with no extra effort. This can be seen to be correct in the following way:

```julia
julia> x = DateTime("2020-06-01T00:01:02.693", DateFormat("yyyy-mm-ddTHH:MM:SS.sssZ"))
julia> datetime2epoch(x)
1590969662693000000

```

```python
>>> import pandas as pd
>>> pd.Timestamp(1590969662693000000)
Timestamp('2020-06-01 00:01:02.693000')

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [July 10, 2020, 8:08am UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/11 "2020-07-10T08:08:10Z")

</div>

A more general version could look like this:

```julia
datetime2epoch(::Type{Millisecond}, x::DateTime) = Millisecond(Dates.value(x) - Dates.UNIXEPOCH)
datetime2epoch(::Type{Second}, x::DateTime) = Dates.value(datetime2epoch(Millisecond, x)) / 1000
datetime2epoch(::Type{T}, x::DateTime) where {T <: TimePeriod} = datetime2epoch(Millisecond, x) |> T

```

Usage:

```julia
>>> dt = DateTime("2020-06-01T00:01:02.693", DateFormat("yyyy-mm-ddTHH:MM:SS.sssZ"))
>>> datetime2epoch(Second, dt) # float - seconds can be noninteger
1.590969662693e9
>>> datetime2epoch(Nanosecond, dt) # Millisecond and smaller give the corresponding period type
1590969662693000000 nanoseconds

```

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 10, 2020, 8:47am UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/12 "2020-07-10T08:47:37Z")

</div>

Hmmm so if you use `::Type{Second}` you get back type `Float64` but if you use `::Type{Millisecond}` you get back type `Millisecond`? 🤔 Doesn’t that seem surprising from a user perspective?

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [July 10, 2020, 8:51am UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/13 "2020-07-10T08:51:35Z")

</div>

Unfortunately, yes - this is because `Second` and other types do not support floating point values.

---

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [July 10, 2020, 9:23am UTC](https://discourse.julialang.org/t/convert-dates-datetime-to-int64-since-epoch/42802/14 "2020-07-10T09:23:25Z")

</div>

If Second doesn’t support floating point values in that case wouldn’t it make more sense to floor it or round it? I mean, `DateTime` has more time resolution than `Second`, and the user must be aware of this. When the user writes `::Type{Second}` what does he expect? Does he expect the correct type with second precision loss, or does he expect a different type with floating precision loss?
