No method matching length(::DateTime)

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

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)?

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/:

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

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.

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,

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

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

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.