Q: comparing date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
Since I am learning C# now, I might have a simple question, I know my
datarow dr[myDate] keeps datatime and I need to compare it if it is current
date, if not how many days ago? How should I compare date and find how many
day difference?
Thanks,
Jim.
 
Assuming your date in the row is XXX as a DateTime object:

TimeSpan ts = DateTime.Now - XXX;

Now you can look at ts properties such as TotalMinutes, etc.

Amil
 
Thanks for the reply.
DateTime sDate=System.Convert.ToDateTime(dr["myDate"]);

TimeSpan ts = DateTime.Now - sDate;
int tHours=ts.Hours;
int tDays=ts.Days;

This is working fine to see discrepancy in hours. Now I only need to compare
date part of DateTime for both dr["myDate"] and DateTime.Now. how should I do
this?
Thanks,
Jim.


Amil said:
Assuming your date in the row is XXX as a DateTime object:

TimeSpan ts = DateTime.Now - XXX;

Now you can look at ts properties such as TotalMinutes, etc.

Amil

JIM.H. said:
Hello,
Since I am learning C# now, I might have a simple question, I know my
datarow dr[myDate] keeps datatime and I need to compare it if it is current
date, if not how many days ago? How should I compare date and find how many
day difference?
Thanks,
Jim.
 
DateTime sDate=System.Convert.ToDateTime(dr["myDateTime"]);

if (sDate.Date < DateTime.Now.Date)
{
debug= "Warning: old date!";
}

do you see any problem with this comparision, it seems it is working?
Thanks,
Jim.

Kevin Spencer said:
Hi Jim,

You would use the methods of the DateTime object:

http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

JIM.H. said:
Hello,
Since I am learning C# now, I might have a simple question, I know my
datarow dr[myDate] keeps datatime and I need to compare it if it is
current
date, if not how many days ago? How should I compare date and find how
many
day difference?
Thanks,
Jim.
 
Back
Top