Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
            DateTime date = (DateTime)com.ExecuteScalar();    
            if(date!=DateTime.Now.Date)
{

}

here i want just to compare date, not the time., it takes datetime so i am unable to compare only date...
for example:
C#
 if(11/21/2012 != 11/22/2012)
{
        //
}
Posted
Updated 21-Nov-12 23:25pm
v2

Use DateTime.Date Property[^] to extract the date component of a DateTime value.

Check this link
How to compare only Date without Time in DateTime types in C#?[^]
 
Share this answer
 
Try:
C#
DateTime date = (DateTime)com.ExecuteScalar();
if(date.Date != DateTime.Now.Date)
   {
The DateTime.Date property returns the date, with the time part set to zero: I.e. Midnight. If you use Date on both sides of the comparison, it is the same as comparing without the time.
 
Share this answer
 
Comments
sumit kausalye 22-Nov-12 10:24am    
i dont want time i just want to compare the dates
OriginalGriff 22-Nov-12 11:50am    
A DateTime always holds a Time as well as a Date part - the clue is in the name.
All that the Date Property does is equalize the Time part of both sides of the comparison, so it is just as if they were not there. I.e. as if you were comparing Dates without Times.
Hi,

Try the below code to compair.
C#
if(dtOne.Date == dtTwo.Date)

Refer the below link.

Date Compair[^]

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/45b6164d-68db-4430-9342-cb2d266c94ff/[^]
 
Share this answer
 
You can use the 'Date property of a DateTime value to extract only the Date, ignoring the time:
DateTime rightNow = DateTime.Now;

DateTime oneMinuteAfterRightNow = rightNow.AddMinutes(1);

Boolean isSameDate = rightNow.Date == oneMinuteAfterRightNow.Date;

See: [^]

Note: in the above example if the first DateTime was at 11:59PM, then adding one minute would move the Date of the second DateTime forward by a day, and the two dates would not be identical.
 
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