SharePoint DateTime default value respect timezone

10,396

Sharepoint stores all date field in UTC. That is, if you are saving a datetime field in a list, Sharepoint actually converts the time that you selected into UTC, and converts it back to whatever time zone the person is in when retrieving.

Begin by checking if there's a difference between your server timezone and your client. When using any API SharePoint will always return the UTC time and leave it up to you to make the conversion in your interface or application.

If you like to solve this differently you can't use the datetime datatype, instead store dates in text fields.

== UPDATE ==

If you got a date from SharePoint (always in UTC) and simply want to convert it to your local time this is a good practice

DateTime localDateTime = sharePointDate.ToLocalTime();

If you got the date as a string, or are uncertain of the formatting and whatnot this can be a good approach

string dateStr = ("2/18/2012 9:49:51 PM").ToString(CultureInfo.InvariantCulture);
DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(dateStr), DateTimeKind.Utc);
DateTime localDateTime = convertedDate.ToLocalTime();
// Outputs 2/18/2012 10:49:51 PM since I'm +1 in sweden
Share:
10,396
Matt Shaver
Author by

Matt Shaver

Updated on June 04, 2022

Comments

  • Matt Shaver
    Matt Shaver almost 2 years

    I have a DateTime field (Date Only) that uses the "Today's Date" as a default value. However, when a user adds an item to the list the date that is selected is respective to the GMT timezone.

    Therfore if a user adds an item on 2/22/2012 at 7pm (EST) the default value will actually read 2/23/2012.

    My first thought was to check the web application settings, and they were set to -5 EST. Then after some searching I found that time zones can differ based on a users regional settings. Testing this approach, I manually set a user to have a EST time zone, and performed the test that yielded the same result.

    Finally, after more searching I found one article that said to use the Calculated Value portion of the DateTime field and specify "=NOW()"

    This however returned "The formula contains a syntax error or is not supported."

    Does anyone know of a way to have the default value of a DateTime field respect the time zone of the web application or the user?

    Thank you.

  • Matt Shaver
    Matt Shaver about 12 years
    I've verified the web application and client are on the same time zone. So is this just a "feature" of SharePoint? To further describe the example create a list with a datetime field, showing DateOnly, and default value of "Today's Date". Then add an item to that list tomorrow 2/23/2012 at 00:05:00. Are you saying no matter what, the date shown will be relative to UTC time? So therefore show 2/22/2012? (I'm going off of the Sweden timezone in your profile)
  • Eric Herlitz
    Eric Herlitz about 12 years
    Heh, ye, as a swede I know about datetime conversions :) But your assumption is correct, the benefit is that you always know that the time comes back in a neutral mode, UTC. I've updated the example with a solution now that things are more clear in what you want!