I have dateTimePicker1 and dateTimePicker2 controls loading on Form1. They both have the same date and time on load.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker2.CustomFormat = "yyyy-MM-dd hh:mm:ss"
When I check if they have different values using
if (dateTimePicker1.Value < dateTimePicker2.Value) {
Console.WriteLine(dateTimePicker1.Value + " is earlier than " + dateTimePicker2.Value);
}
the statement returns true and writes to the console. This is not what I would expect. I would expect this to return false.
If I increase each control's value by 1 second, causing them to still match, the statement returns false as expected and nothing is written to the console.
Why does the less than evaluation return true on load when both values are identical?
Do not know how you are loading the values. But, depending on what precision you are looking for (eg. in hours, minutes or second) you can subtract the two values and compare. Example: If you need precision in seconds then you can do something similar to below: