-1

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.

4

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

1

Timezones vary between eachother.

  • 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 – Kenneth Van Coppenolle 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); – Kenneth Van Coppenolle Oct 31 '12 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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