Results 1 to 5 of 5

Thread: better way to compare dates

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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)
    {
        
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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).

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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
  •  



Click Here to Expand Forum to Full Width