Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 4th, 2006, 10:36 PM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
C# Datetime data types

I have 2 questions about datetimes in C#. First, I want to compare the current date and time to a date and time I have previously defined to see if they match. I compare the dates once a minute using a timer. My problem is the 2 times never match because the seconds don’t match. When I define the datetime I want to compare to the current datetime, I set the seconds to 00, but when the timer compares the current time to that, chances are it wont be right on the minute so they won't match. Here is the code I am using:
// "date" is my already defined date
if (System.DateTime.Now == date)
{
     // do stuff
}
Is there a way to compare the date and time excluding the seconds? I figured out a way to do it, but I was hoping to do it without such a big If statement. Here is how I did it:
// "date" is my already defined date
if ((System.DateTime.Now.Year == date.Year)
                && (System.DateTime.Now.Month == date.Month)
                && (System.DateTime.Now.Day == date.Day)
                && (System.DateTime.Now.Minute == date.Minute))
{
     // do stuff
}

My second question is how do I subtract say 10 minutes off an already defined datetime variable?

Thanks in advanced!
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Jun 4th, 2006, 10:44 PM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
I can't think of an alternative to what you have other than putting your year, month, day and minute into a variables and then comparing them. The statement you have isn't too bad. I would say go ahead and use it.

As for your second question. To subtract 10 minutes, there is no subtract method. Fortunately, there are AddYears, AddMonths, AddHours, etc.
You should be able to just do Addminutes(-10). Example:

date.AddMinutes(-10); //Subtracts 10 minutes

That's the way it's done in my book. That's how I remember it
Booooze is offline   Reply With Quote
Old Jun 4th, 2006, 11:21 PM   #3
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
That will work just fine, thanks for the quick reply.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC