# How to get the number out of a date?

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:

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

Results:

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

Thanks!

Maybe

``````julia> Date(2001, 12, 5).instant.periods.value
730824
``````
1 Like
``````julia> using Dates

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

julia> Dates.value(a)
730824
``````
1 Like

Both methods work! Many thanks, All.

1 Like

Note that this method will not work for all datetimes:

``````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?

2 Likes

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.

``````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.
``````

I think you’re looking for `unix2datetime` and `datetime2unix` (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?

1 Like

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.

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 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.

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

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

It works like a charm! Thank you so much 1 Like