Time comparison (CompareTo) accuracy of datetime

Source: Internet
Author: User
Tags datetime

If the current time is July 19, 2007 13:56 47 seconds
Convert.todatetime ("07/07/19"). CompareTo (DateTime.Now) The value returned at this time is "-1"
Result: Convert.todatetime ("07/07/19") represents a time less than DateTime.Now
Reason
The time represented by Convert.todatetime ("07/07/19") is: 2007-7-19 0:00:00
DateTime.Now the time indicated: 2007-7-19 13:56:47

If we now only do the exact "day" time comparison, then the two time to the same precision.
Method
Convert.todatetime ("07/07/19"). CompareTo (Convert.todatetime (DateTime.Now.ToString ("Yy/mm/dd")) The value returned at this time is "0"
Result: two times equal

Conclusion
When comparing two times in datetime, pay attention to the accuracy of two times.FeedBack:1059370 #1楼 2007-07-19 14:41 black0[unregistered users] Oh, so it is.
Reply Reference #2楼 2007-08-02 10:57 brooklet[unregistered user] Convert.todatetime ("07/07/19"). CompareTo (DateTime.Now.Date)
Reply reference #3楼 [landlord] 2007-08-02 22:57 pcjim @brooklet
It is also a good way to get the date part of the current time with date, thanks for pointing out.

Reply Reference View #4楼 2008-01-09 11:20 qwq[unregistered user] int v = dateTimePicker1.Value.CompareTo (datetimepicker2.value);

if (!) ( DateTimePicker1.Value.Equals (Datetimepicker2.value))
{
if (V > 0)
{
MessageBox.Show ("Delivery time can not advance with the pick-up time ...") ");
return false;
}
}



I would like to ask you to express your views.
(The problem that arises is the time and time error)
Reply reference #5楼 [landlord] 2008-01-31 06:36 pcjim @qwq
Datetimepicker1.value {2008/01/31 5:30:42} System.DateTime
Date {2008/01/31 0:00:00} System.DateTime
Day int
DayOfWeek Thursday System.dayofweek
dayofyear int
Hour 5 int
Kind Local System.datetimekind
Millisecond 296 int
Minute int
Month 1 int
Second int
Ticks 633373542422968750 Long
TimeOfDay {05:30:42.2968750} system.timespan
Year 2008 INT

Datetimepicker2.value {2008/01/31 5:30:42} System.DateTime
Date {2008/01/31 0:00:00} System.DateTime
Day int
DayOfWeek Thursday System.dayofweek
dayofyear int
Hour 5 int
Kind Local System.datetimekind
Millisecond int
Minute int
Month 1 int
Second int
Ticks 633373542422500000 Long
TimeOfDay {05:30:42.2500000} system.timespan
Year 2008 INT

The above is the value of the datetimepicker1.value and datetimepicker2.value monitored after the breakpoint is added, and the surface {2008/01/31 5:30:42} is consistent with {2008/01/31 5:30:42}. But millisecond:296 > 250, this may be the cause of "time and error".
Modified as follows:
int v = dateTimePicker1.Value.Date.CompareTo (dateTimePicker2.Value.Date);
if (!) ( DateTimePicker1.Value.Date.Equals (dateTimePicker2.Value.Date))
{
if (V > 0)
{
MessageBox.Show ("Delivery time can not advance with the pick-up time ...") ");
return false;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.