# Date to float

**URL:** <https://discourse.julialang.org/t/date-to-float/20094>\
**Category:** General Usage\
**Tags:** dates, convert\
**Created:** [January 25, 2019, 3:31pm UTC](https://discourse.julialang.org/t/date-to-float/20094 "2019-01-25T15:31:16Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [January 25, 2019, 3:31pm UTC](https://discourse.julialang.org/t/date-to-float/20094/1 "2019-01-25T15:31:16Z")

</div>

Is there an easy way to convert date/datetime to a float representing year? I.e. f(2000-01-01) = 2000, f(2015-07-01) ~ 2015.5 and so on. The most obvious method is `2000 + (x - DateTime(2000, 1, 1)) / (DateTime(2001, 1, 1) - DateTime(2000, 1, 1))`, but it is only approximate, of course, and becomes more and more wrong for dates far from the reference point (2000 in this case).

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [January 25, 2019, 4:05pm UTC](https://discourse.julialang.org/t/date-to-float/20094/2 "2019-01-25T16:05:19Z")

</div>

I sometimes (in PyPlot surface plots) use this:

```julia
(y,m,d) = (Dates.year.(dN),Dates.month.(dN),Dates.day.(dN))
DaysInMonth = Dates.daysinmonth.(dN)         
RelDayInMonth = (d.-1)./DaysInMonth              
utDatum = y + (m.-1)/12 + RelDayInMonth*1/12

```

---

<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:** [January 25, 2019, 4:37pm UTC](https://discourse.julialang.org/t/date-to-float/20094/3 "2019-01-25T16:37:55Z")

</div>

```julia
const DTM = Union{Date, DateTime}
# assuming you want 2000-01-01 to become 2000.0
yfrac(dtm::DTM) = (dayofyear(dtm) - 1) / daysinyear(dtm)
decimaldate(dtm::DTM) = year(dtm) + yfrac(dtm)

```

CAUTION: `decimaldate` is inexact.

---

<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:** [January 25, 2019, 6:00pm UTC](https://discourse.julialang.org/t/date-to-float/20094/4 "2019-01-25T18:00:48Z")

</div>

Thanks, this looks like exactly what I need! What do you mean by inexact?

---

<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:** [January 25, 2019, 6:41pm UTC](https://discourse.julialang.org/t/date-to-float/20094/5 "2019-01-25T18:41:27Z")

</div>

Using floating point values rather than rational values to represent a wide swath of highly resolved times is inherently inexact.

---

<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:** [January 27, 2019, 8:01am UTC](https://discourse.julialang.org/t/date-to-float/20094/6 "2019-01-27T08:01:24Z")

</div>

You are representing a real number using a binary floating point representation

Binary floating point representation cannot even represent 0.1 (aka 1/10) exactly!!!

```julia
julia> 0.1 + 0.1 + 0.1
0.30000000000000004

"using Precision Decimal Floating Point module (not avail to public)"
julia> using PDFPs

julia> PDFP_setDefaultPrecision(4)
4

julia> PDFP("0.1") + PDFP("0.1") + PDFP("0.1")
PDFP(0, -1, [3, 0, 0, 0])

julia> PDFP_toCommonString(ans)
"0.3000"

julia> PDFP_toExplainationString( PDFP("0.1") + PDFP("0.1") + PDFP("0.1") )
"+1 * 3.000 * 10^-1"

```

---

<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:** [January 27, 2019, 8:06am UTC](https://discourse.julialang.org/t/date-to-float/20094/7 "2019-01-27T08:06:21Z")

</div>

Sure, but it is inherent to all float values, and not related to conversion from date.

---

<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:** [September 10, 2021, 4:49pm UTC](https://discourse.julialang.org/t/date-to-float/20094/8 "2021-09-10T16:49:37Z")

</div>

In case anyone encounters this thread looking for a solution.  
I published a package [Alexander Plavin / DateFormats.jl · GitLab](https://gitlab.com/aplavin/DateFormats.jl) (registered in General) earlier this year. It converts Dates and DateTimes to/from a range of formats, including decimal year (e.g. 2021.45). The implemented conversion is more accurate than the one posted above.

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [July 22, 2024, 4:23pm UTC](https://discourse.julialang.org/t/date-to-float/20094/9 "2024-07-22T16:23:34Z")

</div>

`DateFormats.jl` has moved from `GitLab` to `GitHub` and can be found here: [GitHub - JuliaAPlavin/DateFormats.jl](https://github.com/JuliaAPlavin/DateFormats.jl)
