# Interpolate Dates

**URL:** https://discourse.julialang.org/t/interpolate-dates/8091
**Category:** General Usage
**Tags:** dates, interpolations
**Created:** [January 1, 2018, 11:54am UTC](https://discourse.julialang.org/t/interpolate-dates/8091 "2018-01-01T11:54:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)
#### Post date: [January 1, 2018, 11:54am UTC](https://discourse.julialang.org/t/interpolate-dates/8091/1 "2018-01-01T11:54:47Z")

</div>

Similar to [this](https://discourse.julialang.org/t/linspace-with-datetime/7565), I am interested in writing an interpolation for Date(Times).

```julia
julia> using Interpolations

julia> dates = [Dates.today()+Dates.Year(i-1) for i in 1:6]
6-element Array{Date,1}:
 2018-01-01
 2019-01-01
 2020-01-01
 2021-01-01
 2022-01-01
 2023-01-01

julia> values = [Float64(i-1) for i in 1:6]
6-element Array{Float64,1}:
 0.0
 1.0
 2.0
 3.0
 4.0
 5.0

julia> int = interpolate((dates,),values,Gridded(Linear()));

julia> int[Dates.today()+Dates.Month(6)]
ERROR: MethodError: no method matching length(::Date)
Closest candidates are:
  length(::SimpleVector) at essentials.jl:256
  length(::Base.MethodList) at reflection.jl:558
  length(::MethodTable) at reflection.jl:634
  ...
Stacktrace:
 [1] getindex(::Interpolations.GriddedInterpolation{Float64,1,Float64,Interpolations.Gridded{Interpolations.Linear},Tuple{Array{Date,1}},0}, ::Date) at C:\Users\Eric\.julia\v0.6\Interpolations\src\gridded\indexing.jl:96

```

Any ideas? 😅

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [January 3, 2018, 5:42pm UTC](https://discourse.julialang.org/t/interpolate-dates/8091/2 "2018-01-03T17:42:07Z")

</div>

Looks like it’s mainly because `length(::Date)` isn’t defined. You could do `eval(Base, :(length(::Date) = 1))` and see how far you get after that. If that’s all that’s needed, we could add that definition to `stdlib/Dates`, since I think we define that for Numbers anyway.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 3, 2018, 5:45pm UTC](https://discourse.julialang.org/t/interpolate-dates/8091/3 "2018-01-03T17:45:12Z")

</div>

Numbers are iterable though. Maybe that makes a difference.

---

<div class="post-metadata">

### Author: ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)
#### Post date: [September 3, 2021, 6:34am UTC](https://discourse.julialang.org/t/interpolate-dates/8091/4 "2021-09-03T06:34:47Z")

</div>

I am also interested in interpolation with DateTime as the x-variable.

```julia
using Interpolations, Dates

a = [DateTime(2018,1,1)+Hour(j) for j in 1:6]
println(a)

y = [3*j+1 for j in 1:6]

myinterp = LinearInterpolation(a, y)

```

ERROR: MethodError: no method matching (::Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}})(::DateTime)  
Use square brackets for indexing an Array.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [September 3, 2021, 6:56am UTC](https://discourse.julialang.org/t/interpolate-dates/8091/5 "2021-09-03T06:56:46Z")

</div>

To solve your immidiate problem, you can:

```julia
julia> a2 = Dates.value.(a .- DateTime(2018,1,1))
6-element Vector{Int64}:
  3600000
  7200000
 10800000
 14400000
 18000000
 21600000

julia> myinterp = LinearInterpolation(a2, y)

```

which works.

But yeah, it would be good to either have this info somewhere visible or be able to interpolate date-times somehow.

---

<div class="post-metadata">

### Author: ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)
#### Post date: [September 3, 2021, 7:29am UTC](https://discourse.julialang.org/t/interpolate-dates/8091/6 "2021-09-03T07:29:56Z")

</div>

Thanks!

I guess there should be a layer which maps DateTime to number if DateTime is detected.
