# How to convert Milliseconds to Int64?

**URL:** <https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 14, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218 "2022-07-14T13:38:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 14, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/1 "2022-07-14T13:38:52Z")

</div>

The following is not working:

```julia
julia> diff=Dates.Millisecond(55)
55 milliseconds

julia> convert(Int64, diff)
ERROR: MethodError: Cannot `convert` an object of type Millisecond to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::Gray24) where T<:Real at ~/.julia/packages/ColorTypes/1dGw6/src/conversions.jl:114
  convert(::Type{T}, ::Gray) where T<:Real at ~/.julia/packages/ColorTypes/1dGw6/src/conversions.jl:113
  convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at ~/packages/julias/julia-1.7/share/julia/base/twiceprecision.jl:262
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[19]:1

julia> 

```

How can I make this work?

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 14, 2022, 1:45pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/2 "2022-07-14T13:45:59Z")

</div>

You can access the `value` field of any `TimePeriod` type which should already be an `Int64`

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [July 14, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/3 "2022-07-14T13:48:55Z")

</div>

```julia
julia> diff = Dates.Millisecond(55)
55 milliseconds

julia> Dates.value(diff)
55

julia> typeof(Dates.value(diff))
Int64

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 14, 2022, 1:51pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/4 "2022-07-14T13:51:45Z")

</div>

A better error message when trying to use convert would be nice, though…

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [July 14, 2022, 3:36pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/5 "2022-07-14T15:36:45Z")

</div>

Maybe a PR for better documentation? I did a help in the REPL for Date and DateTime and none of the examples indicated how to extract the values from the representation.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 14, 2022, 4:24pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/6 "2022-07-14T16:24:33Z")

</div>

Perhaps define `Base.convert(Int, x::Period) = value(x)` inside Dates.  
That way there is no need to export `value`, and people who see `convert` generally know there is some type&value transformation happening.

```julia
using Dates

Base.convert(::Type{Int64}, x::Period) = Dates.value(x)
Base.convert(::Type{Integer}, x::Period) = Dates.value(x)

```

---

<div class="post-metadata">

**Author:** ![pitipatw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitipatw/32/211438_2.png) [@pitipatw](https://discourse.julialang.org/u/pitipatw)\
**Post date:** [December 6, 2024, 10:39pm UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/7 "2024-12-06T22:39:16Z")

</div>

Is there any updates on this (Dec 2024). The current solution works fine, but I do agree that a better error message and/or better documentation would help tremendously.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 7, 2024, 6:02am UTC](https://discourse.julialang.org/t/how-to-convert-milliseconds-to-int64/84218/8 "2024-12-07T06:02:52Z")

</div>

A better way to get the same result might be through the Uniful package. The Unitful package is in general good to have in any real-world data setting, so there is mostly upside. Specifically:

```julia
julia> using Unitful

julia> diff = Dates.Millisecond(55)
55 milliseconds

julia> ustrip(diff)
55

julia> ustrip(diff) |> typeof
Int64

```

`ustrip` removes the unit part of any uniful quantity and leaves a scalar.
