Update date of datetime object when using datetime.addhours (-1)

I have a datetime object that I want to remove one hour to display the corect time in a different timezone. I am using datetime.addhours (-1) where -1 is the config value. This works most of the time, except when I set the time to 12:59 pm today, it displays 11:59 am today. When it should display 11:59 pm. Is it possible for addDays () to consider a date?

+1


source to share


4 answers


How about using the Subtract function:



DateTime.Now.Subtract(new TimeSpan(1, 0, 0));

      

+1


source


There is a method that can help you with time zones:

TimeZoneInfo.ConvertTime(..)

      



Communication

+2


source


I noticed that you didn't mention which version of the framework you are using. If you are using 2.0 SP1, 3.0 SP1, or 3.5 SP1, you can use the DateTimeOffset structure . Then you just specified the timezone offset and everything should work.

Hope it helps!

+1


source


Generally speaking, if you are handling timezone problems, you should use functions that store the datetime in UTC and then display the correct TZ.

However, if this is overkill for your application, what you do is correlation.

DateTime datetime = new DateTime( 2008,12,17,0,59, 0 );
datetime = datetime.AddHours(-1);

      

This results in 23:59:00 or 11:59 PM on 12/16/2008.

The times you feed may not have a proper am / pm indication.

+1


source







All Articles