davidweitzenfeld
davidweitzenfeld

Reputation: 1041

DateTime.AddYear, AddMonth, AddDay, AddHour, AddMinute doesn't add?

I have the following code:

DateTime endTime = new DateTime(01, 01, 01, 00, 00, 00);

endTime = endTime.AddYears(currentYear - 1);
endTime = endTime.AddMonths(currentMonth - 1);
endTime = endTime.AddDays(currentDay - 1);

hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);
minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);

endTime = endTime.AddHours(hourToWaitTo);
endTime = endTime.AddMinutes(minuteToWaitTo);

But it doesn't add anything to endTime

EDIT1:

I set currentYear, currentMonth and currentDay like this:

int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));  
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM")); 
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));

hourToWaitTo and minuteToWaitTo is set by user in a textbox.

I want the user to set a time (e.g. 12:25) for the computer to shutdown at, and I also want a countdown to say how many hours:minutes:seconds left till shutdown. I have managed to do all of this, but i couldn't fix the above mentioned endTime problem.

SOLUTION:

The solution to this problem is very simple:

DateTime endTime = new DateTime(currentYear, currentMonth, currentDay, hourToWaitTo, minuteToWaitTo, 0);

I tried to do this earlier, but for some reason I was getting an error. To set those variables above I used:

int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));  
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM")); 
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));

and

int minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);
int hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);

Thank you all for your help.

Upvotes: 0

Views: 2575

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241890

Code like this should be abolished:

int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));  
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM")); 
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));

You are checking the system clock three times, pulling out partial values, serializing to a string, parsing that string, and then using each part. Lots of work for nothing.

All you really need is:

DateTime endTime = DateTime.Today.AddHours(hourToWaitTo)
                                 .AddMinutes(minuteToWaitTo);

You should consider the kind of the dates you are working with. When you construct a DateTime using the constructors, you are getting a .Kind of Unspecified unless you specifically tell it what kind of date you want. It's more appropriate in your scenario to be working with a local date, which you will get with DateTime.Today or DateTime.Now.

Also be aware that since you are asking the user for a local time, but allowing them to enter the time components, that time may be invalid or ambiguous. This happens during daylight savings time transitions. You can validate the user input with TimeZoneInfo.Local.IsInvalidTime() or TimeZoneInfo.Local.IsAmbiguousTime(). In the case of ambiguous time, you will need to ask your user "Before or after the daylight savings transition?" or something similar.

And finally, if there's any chance that the user is NOT in the same timezone as the computer in question, then you have a lot of more work to do. You should consider using DateTimeOffset instead, and you will need to capture the intended offset or timezone of the shutdown. Another approach would be to convert the time to the UTC time of the shutdown event. Review this article for more details and best practices.

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32596

This is not a direct answer to your question - the code you've posted looks okay so there must be something else going on - but I'm wondering why you don't just do something like:

hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);
minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);

DateTime endTime = new DateTime(currentYear, currentMonth, currentDay, hourToWaitTo, minuteToWaitTo, 0);

Upvotes: 1

spender
spender

Reputation: 120518

 new DateTime(year,month,day,hour,minute,0)

?

Upvotes: 0

Related Questions