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