Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Why when I Adding a Data Veriable I must correct them by adding some hours to them. When I using ToUniversalTime().AddHours(2.0) i must add a two hours, and if I using ToLocalTime().AddHours(-1.0) I must minus One Hour.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Maybe you can use something like this:

using (SPSite site = new SPSite("siteurl"))
{
     using (SPWeb web = site.OpenWeb())
     {
          SPUser user = web.CurrentUser;
          SPRegionalSettings settings = user.RegionalSettings;
          if(settings != null){
              SPTimeZone zone = user.RegionalSettings.TimeZone;
              DateTime UTCTime = zone.LocalTimeToUTC(DateTime.Now);//Convert to UTC
              DateTime LocalTime = zone.UTCToLocalTime(UTCTime);//Convert to local time
          }
      }
  }

So to convert a value from the user's timezone, convert it to UTC with zone.LocalTimeToUTC and to convert it back, use UTCToLocalTime.

Warning: I guess this will take the regional settings of the user in SHAREPOINT, not from your computer... So if you move to another timearea, you'll still convert to regionalsettings in the previous region...

Also, I don't know how this will work with winter or summer-time...

share|improve this answer

Timezones vary between eachother.

share|improve this answer
    
Yes I know It, but it isnt Any C# class without timezones ? –  Grzegorz Z Oct 31 '12 at 12:40
    
I must say I failed to understand your question. Can you be more specific? –  Rafał Saltarski Oct 31 '12 at 12:44
1  
AFAIK DateTime defaults to Coordinated Universal Time (UTC). Normally, when you use .ToLocal() it should correctly account for daylight savings and whatnot, and it should return the correct date and time. If that's not working, I guess maybe your system doesn't have the correct timezone set? check Site Settings -> Regional Settings –  Spongeroberto Squarepantalones Oct 31 '12 at 12:53
    
I asking about the method how I should get corect timezone, without any Adding, or decrease. Is there any methods without in C# for dete for only days, or with hours only 00:00 –  Grzegorz Z Oct 31 '12 at 13:01
1  
When you create a datetime object, try explicitly mentioning the timezone by using DateTimeKind.Local: DateTime mydate = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local); –  Spongeroberto Squarepantalones Oct 31 '12 at 13:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.