Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii i am facing a problem in my code in which i am comparing today's date with the date in database this is my code. Here label10.Text is containing the date from database. Basically i am making a reminder app. If the database date matches with todays date then it should send a reminder email
DateTime dt = DateTime.Parse(label10.Text);
            long t = dt.ToFileTime();
            DateTime date1 = DateTime.Today;
            DateTime date2 = new DateTime(t);
            //DateTime date2 = new DateTime(Convert.ToInt64(label10.Text));
            //DateTime date2 = new DateTime(2015,9,16);
            int result = DateTime.Compare(date1, date2);
            string relationship;
            if (result > 0)
            {
                relationship = "is earlier than";
                //SendEmail();               
            }
            else if (result == 0)
            {
                relationship = "is the same time as";
                SendEmail();
                //MessageBox.Show("is same");
            }
            
            else
            {
                relationship = "is later than";
                SendEmail();
                //MessageBox.Show("is later");
            }
            Console.WriteLine("{0} {1} {2}", date1, relationship, date2);


Now the problem is when i run the application the date2 gets filled with 29.09.15 whereas label 10 shows 30.09.15. i am not getting why it is happening
Posted
Updated 29-Sep-15 21:42pm
v2
Comments
phil.o 30-Sep-15 3:06am    
And what is the problem? You did not define it.
Member 11647575 30-Sep-15 3:40am    
lol.. sorry i forgot to write the problem... when ever i run the program, In date 2 the date which is coming is one day less than the date saved in db.. spouse today is 30.09.15(in date1 todays date is displayed) and in database also a reminder is set to 30.09.15 but when i run the program date2 gets filled with 29.09.15
phil.o 30-Sep-15 4:05am    
You do not need the date2 variable, you already have the dt variable. There is no need to get a filetime from dt to create date2 with. Use dt variable directly.
Member 11647575 30-Sep-15 4:29am    
can you please explain me why date2 was getting filled with the date oneday less than the db date ??
phil.o 30-Sep-15 4:37am    
Yes (you could have got it yourself by just consulting the documentation).
DateTime.ToFileTime return a long value, which is the number of 100-nanoseconds intervals elapsed since January, 1601 1st (UTC).
DateTime constructor accepting a long value constructs a DateTime from the number of 100-nanoseconds intervals elapsed since January, 0001 1st.
Thus, the way you are using these property and constructor is inappropriate.
Sources:
DateTime.ToFileTime Method
DateTime Constructor (Int64)
Moreover, you already have the dt variable. You do not need the date2 one.

1 solution

OK - never compare DateTime values for absolute equality: they are accurate to a tick[^], which is one ten millionth of a second.

That means that in practice there is very, very little chance that your code will ever match equality precisely.

So instead, consider checking the difference:
C#
Timespan diff = date1 - date2;
if (diff.TotalSeconds < 0)
   ...
else if (diff.TotalSeconds == 0)
   ...
else 
   ...
That at least gives you a one-second window in which it will match (or if your timer is not that accurate, use TotalMinutes instead)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900