# Now() - Date(2020) errors

**URL:** <https://discourse.julialang.org/t/now-date-2020-errors/50890>\
**Category:** General Usage\
**Tags:** dates\
**Created:** [November 28, 2020, 6:50am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890 "2020-11-28T06:50:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [November 28, 2020, 6:50am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/1 "2020-11-28T06:50:45Z")

</div>

Hi All,

I am seeing the following behavior with Julia 1.5.2 with Win64.

```julia
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.

regards,

Sambit

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 28, 2020, 7:18am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/2 "2020-11-28T07:18:37Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [November 28, 2020, 7:22am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/3 "2020-11-28T07:22:51Z")

</div>

`Base.:-(::DateTime, ::Date)` should return a `Period` most likely in `Millisecond` as is with `Base:-(::DateTime, ::DateTime)`.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [November 28, 2020, 9:00am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/4 "2020-11-28T09:00:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [November 28, 2020, 9:48am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/5 "2020-11-28T09:48:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [November 28, 2020, 9:52am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/6 "2020-11-28T09:52:29Z")

</div>

Issue added: [https://github.com/JuliaLang/julia/issues/38592](https://github.com/JuliaLang/julia/issues/38592)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 28, 2020, 9:53am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/7 "2020-11-28T09:53:04Z")

</div>

But what time do you want to use for the `Date`? 00.00? Midday? Same time as the time in the other argument?

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [November 28, 2020, 10:00am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/8 "2020-11-28T10:00:16Z")

</div>

`Base:-(dt::DateTime, d::Date) = dt - DateTime(d)` Whatever time the constructor shall convert by default.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 28, 2020, 11:11am UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/9 "2020-11-28T11:11:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 28, 2020, 1:39pm UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/10 "2020-11-28T13:39:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [November 28, 2020, 5:54pm UTC](https://discourse.julialang.org/t/now-date-2020-errors/50890/11 "2020-11-28T17:54:39Z")

</div>

```julia
julia> today()
2020-11-29

julia> now()
2020-11-29T01:52:24.232

julia> today() - Date(2020)
333 days

julia> now() - DateTime(2020)
28777954689 milliseconds

julia> today() - firstdayofyear(today())
333 days

julia> now() - firstdayofyear(now())
28777964880 milliseconds

```

`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.

`Year` is a constructor for a time period.
