Results 1 to 5 of 5

Thread: DateTime problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    DateTime problem

    Hi.

    This is a bit of an issue I found.

    My application has an object which has a property/field of a dateTime value.

    On every tick, the current time is compared with the objects' datetime value and if it succeeds, it does a particular operation.

    Now, thats fine when comparing TIME but when I want to compare the current Date/Time (DateTime.Now) - the condition never evaluates to true with the objects' datetime property

    I can compare time, by specifiying each of the "time" values (Hour, Minute, Second) but I would like to be more efficient, and well generally better, and use the DateTime.Now to compare all factors, or at least the current date.

    I have tried the DateTime.Equals() and the == operator but both seem to evaluate to false when that time approaches.


    There is no code to show at all apart from comparing 2 objects:

    Code:
    if (DateTime.Now == ((SomeClass)this.TheCollection.TheStartTime)) {..}

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: DateTime problem

    Use >= instead, it's highly unlikely that Now() will ever return exactly the same as your intended time when you are calling it from a tick event. Timers can't be that accurate.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: DateTime problem

    yeh i agree. but still - we are talking about the DATE not time and well, DATE will almost always remain constant throughout the Day :P

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: DateTime problem

    Code:
    			if (DateTime.Now.ToShortDateString == yourDate.ToShortDateString)
    			{
    			//code
    			}

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

    Re: DateTime problem

    The DateTime structure has a Date property that returns another DateTime object with the same date and the time zeroed to midnight, so if you want to compare your date to the current date you would use:
    Code:
    if (myDate.Date == DateTime.Today)
    Note that DateTime.Today is like DateTime.Now but it too has the time zeroed to midnight, so DateTime.Today is the same as DateTime.Now.Date. The complement of DateTime.Date is DateTime.TimeOfDay, which returns a TimeSpan object that represents the time since midnight on that date.
    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

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