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

We have got two method in Sharepoint to get the time. One is ToUniversalTime() an secound is ToLocalTime() But when we are in server and have got a aplication instaled on server which kind data method is better ? If wee set a ToLocalTime it will take a time from server or from local machine loging to our aplication ? Please help.

share|improve this question
add comment

1 Answer

this is the difference.

ToUniversalTime does:

return TimeZoneInfo.ConvertTimeToUtc(this, TimeZoneInfoOptions.NoThrowOnInvalidTime);

Instead ToLocalTime does:

  if (this.Kind == DateTimeKind.Local)
    return this;
  bool isDaylightSavings = false;
  bool isAmbiguousLocalDst = false;
  long ticks = this.Ticks + TimeZoneInfo.GetUtcOffsetFromUtc(this, TimeZoneInfo.Local, out isDaylightSavings, out isAmbiguousLocalDst).Ticks;
  if (ticks > 3155378975999999999L)
  {
    if (throwOnOverflow)
      throw new ArgumentException(Environment.GetResourceString("Arg_ArgumentOutOfRangeException"));
    else
      return new DateTime(3155378975999999999L, DateTimeKind.Local);
  }
  else
  {
    if (ticks >= 0L)
      return new DateTime(ticks, DateTimeKind.Local, isAmbiguousLocalDst);
    if (throwOnOverflow)
      throw new ArgumentException(Environment.GetResourceString("Arg_ArgumentOutOfRangeException"));
    else
      return new DateTime(0L, DateTimeKind.Local);
  }

So seems that the ToLocalTime function get the datetime from the server

share|improve this answer
    
So we should set all times and data times in ToLocalTime. Not the ToUniversalTime. And If the time changing to the Sammer time it will change if the sammer time changing in Server ? –  Grzegorz Z Feb 11 '13 at 12:17
    
I should change in all playces to the ToLocalTimes ? I afraid it will be in about 200 places where I used ToUniversalTime –  Grzegorz Z Feb 11 '13 at 12:21
add comment

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.