# How to get the number out of a date?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355>\
**Category:** General Usage\
**Tags:** question, dates\
**Created:** [October 25, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355 "2021-10-25T20:23:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/1 "2021-10-25T20:23:05Z")

</div>

I tried the below, it was able to generate the number I want, i.e., 730824, but how can I extract this number out?

My code:

```julia
using Dates
A = dump(Date(2001, 12, 5))

```

Results:

```julia
Date
  instant: Dates.UTInstant{Day}
    periods: Day
      value: Int64 730824

```

Thanks!

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [October 25, 2021, 8:33pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/2 "2021-10-25T20:33:46Z")

</div>

Maybe

```julia
julia> Date(2001, 12, 5).instant.periods.value
730824

```

---

<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:** [October 25, 2021, 8:34pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/3 "2021-10-25T20:34:28Z")

</div>

```julia
julia> using Dates

julia> a = Date(2001,12,5)
2001-12-05

julia> Dates.value(a)
730824

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 8:37pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/4 "2021-10-25T20:37:38Z")

</div>

Both methods work! Many thanks, All.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 25, 2021, 8:41pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/5 "2021-10-25T20:41:06Z")

</div>

Note that this method will _not_ work for all datetimes:

```julia
julia> using Dates

julia> d = now()
2021-10-25T22:39:14.665

julia> Dates.value(d)
63770884754665

julia> dump(d)
DateTime
  instant: Dates.UTInstant{Millisecond}
    periods: Millisecond
      value: Int64 63770884754665

```

The question is - to what resolution do you want this value? Relative to what, `01-01-1970`? How do you get them and what do you want to do with them?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 8:47pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/6 "2021-10-25T20:47:46Z")

</div>

Thanks. I only need to have some loose idea, so precision to the day is more than enough. I have several decades of sampling data and I just want to see the date in the middle.

BTW, once I get the average datenumber, how do I convert it back to Date? I tried the below and it did not work well.

```julia
A = Date(2001,12,5);
B = Dates.value(A); # get the date number
C = Dates.Date(B); # Trying to convert the date number back to date.

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 25, 2021, 8:53pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/7 "2021-10-25T20:53:51Z")

</div>

I think you’re looking for [`unix2datetime` and `datetime2unix`](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.unix2datetime) (which takes a `DateTime`). Julia has proper datetime arithmetic built in, so you should not have to work with the raw values.

Maybe you can explain what you want to do with them?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 8:56pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/8 "2021-10-25T20:56:38Z")

</div>

I have several decades of sampling data. They have date information stored as 3 separate columns: `Year`, `Month`, and `Day`. What I need to find out is the average date.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 25, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/9 "2021-10-25T21:05:22Z")

</div>

Are you talking about the median, geometric or arithmetic mean…? How do you want to round, in case you don’t get an even integer (truncation, towards/away from zero, towards positive/negative infinity, …)?

Sorry if I come across as intrusive, it’s just hard for me to give a concrete suggestion when I’m not clear what you’re talking about 🙂

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 9:53pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/10 "2021-10-25T21:53:33Z")

</div>

No at all. I really appreciate your help.

My variables are changing smoothly with time. I want to find out the average date value so that I can adjust all data to that date. I think what I need is the arithmetic mean? I can round the results, so that shouldn’t be a problem. For this post, I just want to find out how to convert a date number like 730824 back to a date or its original Year, Monty and Day. Thanks.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 25, 2021, 10:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/11 "2021-10-25T22:17:57Z")

</div>

> [@leon](#):
>
> I want to find out the average date value so that I can adjust all data to that date.

You can use the `Dates.UTD()` function:

```julia
using Dates, Random
d = rand(Date(2021,10,1):Day(1):today(), 10)
d̄ = Date(Dates.UTD(round(Int, mean(Dates.value.(d)))))

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 25, 2021, 10:44pm UTC](https://discourse.julialang.org/t/how-to-get-the-number-out-of-a-date/70355/12 "2021-10-25T22:44:27Z")

</div>

It works like a charm! Thank you so much 👍
