# Linspace with DateTime

**URL:** <https://discourse.julialang.org/t/linspace-with-datetime/7565>\
**Category:** General Usage\
**Tags:** note\
**Created:** [December 6, 2017, 12:44pm UTC](https://discourse.julialang.org/t/linspace-with-datetime/7565 "2017-12-06T12:44:38Z")\
**Posts on this page:** 1\
**Page:** 1

<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:** [December 6, 2017, 12:44pm UTC](https://discourse.julialang.org/t/linspace-with-datetime/7565/1 "2017-12-06T12:44:38Z")

</div>

I wanted to create a linearly spaced range of `DateTime`s, but:

```julia
julia> linspace(DateTime(0), now(), 5)
5-element LinSpace{DateTime}:
Error showing value of type LinSpace{DateTime}:
ERROR: MethodError: no method matching *(::Float64, ::DateTime)

```

So I wrote this:

```julia
function mylinspace(d1::DateTime, d2::DateTime, n::Int)
    Δ = d2 - d1
    T = typeof(Δ)
    δ = T(round(Int, Dates.value(Δ)/(n - 1)))
    d2 = d1 + δ*(n - 1)
    return d1:δ:d2
end

```

which works:

```julia
julia> mylinspace(DateTime(0), now(), 5)
0000-01-01T00:00:00:15919946730059 milliseconds:2017-12-06T13:42:00.236

```

Obviously, this is slightly off but its accuracy is on the order of a few milliseconds, which I can live with.
