Jump to content

c# DateTime Picker - why is the 1st August 2020 earlier than the 26th July 2020?

Dravinian

This one has baffled me, and I can't work out why it is happening at all.


My code:

 

            string date1;
            string date2;


            date1 = datePicker1.Text;
            date2 = datePicker2.Text;
            
            int compareResult1 = date1.CompareTo(date2);

            if (compareResult1 < 0)

 

This works, if you put in the 26/07/2020 and then put in 25/07/2020, it will tell you that the second date is earlier than the first.

 

However, if I put the 27/07/2020 and then put in 01/08/2020 it will tell me that the second date is earlier than the first - which it is clearly not.

 

This is of course localised to my date format, so if you are using a different format, just assume that it is 07/27/2020 compared to 08/01/2020 and it is telling you that 08/01/2020 is earlier.

 

I did a massive amount of searching around DateTime, CompareTo, TryParse etc etc. and a lot of it is very old and refers to using a DatePicker.Value and DateTime...but there is no DatePicker.Value in WPF, and if you try many many many of the methods it comes back "cannot implicity convert to a string"

 

There must be a simple way to compare two dates from a DatePicker and say which one comes first?

 

After what I imagine is two hours of banging my head against Google I come seeking aid...

Link to comment
Share on other sites

Link to post
Share on other sites

But bear in mind, it is now 3.30am so I am going to bed, any assistance will lauded greatly when I wake up.


Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Dravinian said:

There must be a simple way to compare two dates from a DatePicker and say which one comes first?

 

Though I don't program in C#, the easiest way to implement a time/date picker is to use ONLY acceptable date format as per ISO8601 during the comparation stage. (Or you could calculate epoch time instead, since that's simply a fixed number that counts upward from its' starting date/time.)

Desktop: KiRaShi-Intel-2022 (i5-12600K, RTX2060) Mobile: OnePlus 5T | Koodo - 75GB Data + Data Rollover for $45/month
Laptop: Dell XPS 15 9560 (the real 15" MacBook Pro that Apple didn't make) Tablet: iPad Mini 5 | Lenovo IdeaPad Duet 10.1
Camera: Canon M6 Mark II | Canon Rebel T1i (500D) | Canon SX280 | Panasonic TS20D Music: Spotify Premium (CIRCA '08)

Link to comment
Share on other sites

Link to post
Share on other sites

Because you're not comparing dates; you're comparing strings, and the string "01/08/2020” comes before the string "27/07/2020”, because the char code for '0' is less than that of '2'.

 

Use `DateTime.TryParse` to convert the string to an actual `DateTime`, and then compare them.

CPU: AMD Ryzen 9 5900X · Cooler: Artic Liquid Freezer II 280 · Motherboard: MSI MEG X570 Unify · RAM: G.skill Ripjaws V 2x16GB 3600MHz CL16 (2Rx8) · Graphics Card: ASUS GeForce RTX 3060 Ti TUF Gaming · Boot Drive: 500GB WD Black SN750 M.2 NVMe SSD · Game Drive: 2TB Crucial MX500 SATA SSD · PSU: Corsair White RM850x 850W 80+ Gold · Case: Corsair 4000D Airflow · Monitor: MSI Optix MAG342CQR 34” UWQHD 3440x1440 144Hz · Keyboard: Corsair K100 RGB Optical-Mechanical Gaming Keyboard (OPX Switch) · Mouse: Corsair Ironclaw RGB Wireless Gaming Mouse

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Chris Pratt said:

Because you're not comparing dates; you're comparing strings, and the string "01/08/2020” comes before the string "27/07/2020”, because the char code for '0' is less than that of '2'.

 

Use `DateTime.TryParse` to convert the string to an actual `DateTime`, and then compare them.

dammit, I had gotten to the point of using int.TryParse(datePicker1.Text, out date1)

 

Did not, in all of my googling come across DateTime.TryParse.

 

Thank you, implemented and working pefectly.

 

I really do appreciate this actually, I couldn't turn my brain off trying to work out how to do this and I wasn't gonna fall asleep for long time without a fix...so very grateful thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, kirashi said:

Though I don't program in C#, the easiest way to implement a time/date picker is to use ONLY acceptable date format as per ISO8601 during the comparation stage. (Or you could calculate epoch time instead, since that's simply a fixed number that counts upward from its' starting date/time.)

The epoch time isn't a bad idea, and I did think about this and the Universal Time thing that counts up from 01/01/1970 or something (going from memory here they could be the same thing).

 

I just thought there had to be a better way.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×