# Find decimal year from time

**URL:** <https://discourse.julialang.org/t/find-decimal-year-from-time/126670>\
**Category:** General Usage\
**Tags:** dates, time, datetime\
**Created:** [March 7, 2025, 10:22am UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670 "2025-03-07T10:22:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 7, 2025, 10:22am UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/1 "2025-03-07T10:22:51Z")

</div>

Hi,

I’ve searched for something in libraries like Dates and AstroTime, but maybe missed it as I’m surprised not to find it.  
I would like to find the decimal time from a date, hour, minute, second, e.g.:  
DateTime(2018,1,2,9,53,1) would give 2018.003828474404

This would be the reverse function from this thread:

> <https://stackoverflow.com/questions/51432310/julia-convert-float64-to-date-and-to-datetime>

Also, I need to take the leap second into account.

---

<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:** [March 7, 2025, 12:14pm UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/2 "2025-03-07T12:14:35Z")

</div>

Aside from getting the year, which is in Dates module, getting the partial year can be done as follows (note the endless trickiness with dates requires care not to make bugs):

```julia
modyear(d) = 
  (d - firstdayofyear(d)) / (lastdayofyear(d)+Day(1) - firstdayofyear(d))

```

With this function:

```julia
julia> d = DateTime(2025,3,2,10,30,10)
2025-03-02T10:30:10

julia> year(d)
2025

julia> modyear(d)
0.16558250887874176

julia> year(d)+modyear(d)
2025.1655825088787

```

---

<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:** [March 7, 2025, 1:58pm UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/3 "2025-03-07T13:58:52Z")

</div>

The `yeardecimal` function from `DateFormats` goes in both direction, `DateTime` ↔ decimal:

```julia
julia> using DateFormats

julia> dt = DateTime(2018,1,2,9,53,1)
2018-01-02T09:53:01

julia> yeardecimal(dt)
2018.003867992136

julia> yr = 2018.003867992136

julia> yeardecimal(yr)
2018-01-02T09:53:01

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 7, 2025, 5:21pm UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/4 "2025-03-07T17:21:53Z")

</div>

Yay thanks !  
I’ll check if the leap second is managed.

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 10, 2025, 11:55am UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/5 "2025-03-10T11:55:10Z")

</div>

Looks like that the leap second is not managed by this package.  
E.g. a leap second was added the 2005/12/31; but:

```julia
dt = DateTime(2005,12,31,23,59,60)
ERROR: ArgumentError: Second: 60 out of range (0:59)

```

---

<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:** [March 10, 2025, 12:01pm UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/6 "2025-03-10T12:01:53Z")

</div>

I think leap seconds are just not supported by Julia’s DateTimes. Probably on purpose? But idk.  
There can be some packages that add leap seconds support to Julia.

---

<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:** [March 10, 2025, 12:34pm UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/7 "2025-03-10T12:34:17Z")

</div>

> [@aplavin](#):
>
> I think leap seconds are just not supported by Julia’s DateTimes. Probably on purpose? But idk.

Link to [an older thread](https://discourse.julialang.org/t/higher-resolution-datetime-timestamp/3069) with a detailed discussion of this topic.

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [March 11, 2025, 9:15am UTC](https://discourse.julialang.org/t/find-decimal-year-from-time/126670/8 "2025-03-11T09:15:45Z")

</div>

Thanks everyone !
