|
-
Oct 26th, 2006, 07:54 PM
#1
Thread Starter
Fanatic Member
better way to compare dates
hi guys! can anybody please help me...i have the code below which compare two dates,datenow and the alarmdate, and show the alarm notification when they are equal but as you can notice the way of comparing dates is first Convert both dates ToShortDateString and compare it and when they are equal i have to convert again the 2 dates ToShortTimeString and compare it to make sure that the dates and times on both dates are equal..my question is, is there any better way, if there is, to achieve what i'm trying to do. Thanks in advance!
-
Oct 26th, 2006, 08:04 PM
#2
Re: better way to compare dates
If you're comparing dates and/or times then you should be comparing DateTime and/or TimeSpan objects. In .NET 2.0 both support standard mathematical operators:
Code:
// Compare dates.
if (DateTime.Today >= alarmDateTime.Date)
{
}
// Compare times.
if (DateTime.Now.TimeOfDay >= alarmDateTime.TimeOfDay)
{
}
-
Oct 26th, 2006, 08:14 PM
#3
Thread Starter
Fanatic Member
Re: better way to compare dates
Sorry but i have not mentioned that i want to compare dates exlcuding the second and miliseconds just the date and time (HH:MM).
-
Oct 26th, 2006, 08:22 PM
#4
Re: better way to compare dates
Haven't we already covered this?
Code:
DateTime modifiedCurrentTime = new DateTime(currentTime.Year,
currentTime.Month,
currentTime.Day,
currentTime.Hour,
currentTime.Minute,
0);
DateTime modifiedAlarmTime = new DateTime(alarmTime.Year,
alarmTime.Month,
alarmTime.Day,
alarmTime.Hour,
alarmTime.Minute,
0);
if (modifiedCurrentTime >= modifiedAlarmTime)
{
// The alarmTime has been reached.
}
If you want to compare specifically the date or time prtions of those two DataTimes then you can specifically construct DateTime objects from just the date data and TimeSpan objects from just the hour and minute data and then compare using code like that in my previous post.
-
Oct 26th, 2006, 08:40 PM
#5
Thread Starter
Fanatic Member
Re: better way to compare dates
Yah..we already did...sorry I forgot...i'll just search that thread. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|