I am seeing the following behavior with Julia 1.5.2 with Win64.
julia> now() - Date(2020)
MethodError: no method matching -(::DateTime, ::Date)
Closest candidates are:
-(::DateTime, !Matched::Month) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:60
-(::DateTime, !Matched::Year) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:31
-(::DateTime, !Matched::Period) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:77
...
Stacktrace:
[1] top-level scope at In[27]:1
[2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
[3] execute_code(::String, ::String) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:86
[5] #invokelatest#1 at .\essentials.jl:710 [inlined]
[6] invokelatest at .\essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\eventloop.jl:8
[8] (::IJulia.var"#15#18")() at .\task.jl:356
This is kind of non-intuitive. I feel DateTime and Date should be interchangeable where needed. Workaround is of course Date(now()) - Date(2020) or today() - Date(2020) or now() - DateTime(2020) but it may be just simpler if the system had a default behavior than erroring.
Something like that would be implemented by a Base.:-(::Datetime, ::Date) method, but I suppose it doesn’t exist because you can’t generally decide if you want to return a Datetime or a Date.
What you’re looking for is today() - Day(2020). I think the conversion should be made explicit because there is no “right choice to return”, different users would like different things, so prefering one over another is a debate. By making the conversion explicit, people would be clear which one they like.
Updated the description to reflect all possible options to choose. float() - int() returns default to float. So technically it’s not too hard to think of DateTime() - Date() can be mapped to Period in milliseconds.
I think you meant to say now() - Year(2020), as I’m guessing Date(2020) is meant to convey 2020 years ago? But as you say, there is quite a bit of ambiguity here, and it seems inappropriate for a simple method like - to make these decisions implicitly.
If I understand correctly Date(2020) refers to 1st Jan 2020, whereas Year(2020) refers to 2020 years. In this case it appears that OP wants the amount of time that has elapsed since the beginning of this year. That’s a reasonable question to ask, although I suppose now() - Year(2020) would lead to something similar.
Date and DateTime are type constructors respectively, where the year is required, but the other parameters are optional. The last two lines are perhaps more Julian. I think implicit conversion here is not the best idea, as the user should be clear which output they would want.