# No method matching length(::DateTime)

**URL:** <https://discourse.julialang.org/t/no-method-matching-length-datetime/118262>\
**Category:** General Usage\
**Tags:** dates\
**Created:** [August 16, 2024, 9:23am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262 "2024-08-16T09:23:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [August 16, 2024, 9:23am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262/1 "2024-08-16T09:23:35Z")

</div>

I encountered this error message while using `indexin` in a for loop. Here is an example

```julia
using Dates
indexin(DateTime(2024,1), DateTime(2024,1):Day(1):DateTime(2024,2))

ERROR: MethodError: no method matching length(::DateTime)

Closest candidates are:
  length(::Union{Base.KeySet, Base.ValueIterator})
   @ Base abstractdict.jl:58
  length(::Union{SparseArrays.FixedSparseVector{Tv, Ti}, SparseArrays.SparseVector{Tv, Ti}} where {Tv, Ti})
   @ SparseArrays /software/sse2/tetralith_el9/manual/julia/1.9.4/julia-1.9.4/share/julia/stdlib/v1.9/SparseArrays/src/sparsevector.jl:95
  length(::Union{LinearAlgebra.Adjoint{T, <:Union{StaticArraysCore.StaticArray{Tuple{var"#s2"}, T, 1} where var"#s2", StaticArraysCore.StaticArray{Tuple{var"#s3", var"#s4"}, T, 2} where {var"#s3", var"#s4"}}}, LinearAlgebra.Diagonal{T, <:StaticArraysCore.StaticArray{Tuple{var"#s14"}, T, 1} where var"#s14"}, LinearAlgebra.Hermitian{T, <:StaticArraysCore.StaticArray{Tuple{var"#s11", var"#s12"}, T, 2} where {var"#s11", var"#s12"}}, LinearAlgebra.LowerTriangular{T, <:StaticArraysCore.StaticArray{Tuple{var"#s19", var"#s20"}, T, 2} where {var"#s19", var"#s20"}}, LinearAlgebra.Symmetric{T, <:StaticArraysCore.StaticArray{Tuple{var"#s8", var"#s9"}, T, 2} where {var"#s8", var"#s9"}}, LinearAlgebra.Transpose{T, <:Union{StaticArraysCore.StaticArray{Tuple{var"#s2"}, T, 1} where var"#s2", StaticArraysCore.StaticArray{Tuple{var"#s3", var"#s4"}, T, 2} where {var"#s3", var"#s4"}}}, LinearAlgebra.UnitLowerTriangular{T, <:StaticArraysCore.StaticArray{Tuple{var"#s25", var"#s26"}, T, 2} where {var"#s25", var"#s26"}}, LinearAlgebra.UnitUpperTriangular{T, <:StaticArraysCore.StaticArray{Tuple{var"#s22", var"#s23"}, T, 2} where {var"#s22", var"#s23"}}, LinearAlgebra.UpperTriangular{T, <:StaticArraysCore.StaticArray{Tuple{var"#s16", var"#s17"}, T, 2} where {var"#s16", var"#s17"}}, StaticArraysCore.StaticArray{Tuple{var"#s26"}, T, 1} where var"#s26", StaticArraysCore.StaticArray{Tuple{var"#s1", var"#s4"}, T, 2} where {var"#s1", var"#s4"}, StaticArraysCore.StaticArray{<:Tuple, T}} where T)
   @ StaticArrays ~/.julia/packages/StaticArrays/MSJcA/src/abstractarray.jl:1
  ...

Stacktrace:
 [1] _similar_shape(itr::DateTime, #unused#::Base.HasLength)
   @ Base ./array.jl:658
 [2] _array_for(#unused#::Type{Union{Nothing, Int64}}, itr::DateTime, isz::Base.HasLength)
   @ Base ./array.jl:674
 [3] indexin(a::DateTime, b::StepRange{DateTime, Day})
   @ Base ./array.jl:2484
 [4] top-level scope
   @ REPL[64]:1

```

Would it be reasonable to expect Dates to provide a method for `length(::DateTime)`?

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [August 16, 2024, 9:30am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262/2 "2024-08-16T09:30:20Z")

</div>

I’m not sure what you are trying to do here. `indexin` expects two collections and `DateTime` isn’t one - hence the error.

If you just want to check if a date lies in a range use `in`/`∈`:

```julia
DateTime(2024,1) in DateTime(2024,1):Day(1):DateTime(2024,2) # true

```

---

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [August 16, 2024, 10:45am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262/3 "2024-08-16T10:45:28Z")

</div>

In my real problem I needed the index and I know how to circumvent the problem, but there could also be other cases where `length(::DateTime)` could be convenient, e.g. in creating arrays based on the size of variables.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [August 16, 2024, 10:54am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262/4 "2024-08-16T10:54:20Z")

</div>

I think we’re at cross purposes here. `Base.length` is intended to return the number of items in a collection and there isn’t a single obvious interpretation of `DateTime` as a collection.

To find the index of an item in a collection you would typically use `findfirst`/`searchsortedfirst` and related methods. For example,

```julia
r = DateTime(2024,1):Day(1):DateTime(2024,2)
t = DateTime(2024, 1)
searchsortedfirst(r, t) # 1
findfirst((==)(t), r) # 1

```

If you really want to use `indexin` then you must use a collection for the first argument, ie

```julia
indexin([t], r) # [1]

```

---

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [August 16, 2024, 11:50am UTC](https://discourse.julialang.org/t/no-method-matching-length-datetime/118262/5 "2024-08-16T11:50:06Z")

</div>

I agree that `findfirst` may be a good alternative in this specific example.

Personally, I find it convenient that for example `length(1)`, `length([1])` and `length((1,))` all work. In my view, it would be useful for other types like DateTime as well.
