Compare DateOnly values with DateTime. Shouldn't it be possible?
Asked Answered
N

3

10

Using .Net 6 and VS2022 , consider this code:

DateOnly dateOnly= new DateOnly(2022,12,24);
            DateTime dateTime = DateTime.Now;

            if (dateTime > dateOnly)
            {

            }

It will result in this error:

Operator '>' cannot be applied to operands of type 'DateTime' and 'DateOnly'

Even there are no built-in properties to get the DateOnly from a DateTime without coding some custom extension methods nor the DateOnly.Compare methods support comparing to the DateTime type. The story is the same for TimeOnly If I am not missing something, what is the correct way of comparing these two types?

Update:

Just found It is not even possible to use these types just like other types in the webapi query parameters! Also EF core 6 does not have build-in support for these types in SqlClient!
Maybe it would be better to delay using these types...

Non answered 24/12, 2022 at 15:15 Comment(5)
You need o use date property which truncates a date to midnight : DateTime.Now.DateTessler
Nope! It does not work! The Date property is also DateTime and can not be compared to DateOnly.Non
Use Date instead of DateOnly.Tessler
@Tessler There is no such Date datatype in .net6 and 7. Which version/framework are you speaking about?Non
You might be interested to read github.com/dotnet/runtime/issues/58734Broder
M
16

You can get the DateOnly from DateTime like this.

DateOnly dateOnly = DateOnly.FromDateTime(dateTime);

Then you can use the CompareTo method to compare both DateOnly.

Same concept is for TimeOnly, there is a TimeOnly.FromDateTime method.

Mulderig answered 24/12, 2022 at 16:9 Comment(3)
It looks that currently converting before comparing is the only solution. I hope for a better support for these two types in the future versions.Non
@MDZand it will not will be future as well because DateTime represents date plus time, so you cannot directly compare it with only date or only timeMulderig
@VivekNuna sure you can - the exact same way you can generate one from the other and compare them. That is also one of the main motivations for the existence of those types. Without comparisons they are rather unnecessary types.Asuncion
C
1

You can use the following methods to convert either to DateOnly or to DateTime, depending on what you need in the end.

var d = DateOnly.FromDateTime(DateTime.Now);
var dt = d.ToDateTime(TimeOnly.MinValue); // or whatever time you want
Chemotherapy answered 24/12, 2022 at 15:29 Comment(3)
Don't forget the final conversionSheerlegs
You did not addressed my problem: Compare DateTime and DateOnly @ChemotherapyNon
@MDZand your question was "what's the correct way to compare them". And my answer was you need to convert either to DateTime or to DateOnly and then compare.Chemotherapy
S
-5

This can work but you need to do a little fandangling.

I.E. Convert either DateOnly or DateTime to string then convert it to the other format then use compare. Off the top of my head, I do not know the exact code but it will look like this:

DateOnly origDO;// = some value;
DateTime origDT;// = some value;

string dTString = origDT.ToString("yyyy-MM-dd HH:mm:ss");
DateOnly convertedDO = DateOnly.Parse(dTString);

//-1 = <, 0 = ==, 1 = >
DateOnly.Compare(origDO, convertedDO);

The best way, is probably to use the base conversions that gSharp showed and then use the default comparison that I showed.

Don't forget that you can always write your own comparison at any time. So, DateOnly.Day > DateTime.Day

Sheerlegs answered 24/12, 2022 at 15:25 Comment(2)
@Bart The question is not about best practices, it is about what is possible or not. It is not our place to judge what the asker wants, only to provide information or move on. Furthermore, I see no answer from you and no references to official document stating why the above mentioned practices should be mentioned which invalidates your argument.Sheerlegs
I don't know what you're responding to apart from maybe my downvote, but one of the reasons answers on SO have to be taken with a big chunk of salt more often than not is exactly that attitude. Providing an answer just for the sake of it isn't helping in the long run. You could have saved it to a file and then parsed that file, which would have worked as well, but I hope you agree that this isn't a good way to teach.Remanence

© 2022 - 2024 — McMap. All rights reserved.