1

On some websites, when I get an item by

<site-url>/_api/Web/GetFolderByServerRelativePath(decodedurl='<folder-path>/')/listitemallfields

, the API returns the Created and Modified fields in an unknown timezone (presumably the site's timezone) instead of UTC:

"Created": "2019-01-17T18:55:41",
"Modified": "2019-01-17T18:55:41",

However if I get the item by <site-url>/_api/web/lists(<list-id>)/items(<item-id>) the responded times are in UTC:

"Created": "2019-01-18T02:55:41Z",
"Modified": "2019-01-18T02:55:41Z",
  1. When and why does this happen?
  2. How can I get the datetime in a "correct" format? That is, is there any way to force the API to return the times in UTC without changing the site or list's settings, or to request the API to return the time along with its timezone?
0

This is what i was able to find out:

The dates in ListItemAllFields seem to always be in the local time rather than UTC, as you mentioned. I am not sure WHY this happens, but at least seems to happen consistently. If for some reason it doesn't for you, you can tell if the time is UTC or not with the Z at the end of the datestring - if it's here, it's UTC, otherwhise it isn't.

For your second question, I wasn't able to find anything like that. However, there are a few options to work around this:

  1. Use the LocalTimeToUTC endpoint

As the name suggests, this endpoint converts a date string to UTC based on the configured local date of the site.

For example <site-url>/_api/web/RegionalSettings/TimeZone/localTimeToUTC(@Date)?@date='2017-06-12T04:02:17' returns:

{
    "d": {
        "LocalTimeToUTC": "2017-06-12T11:02:17Z"
    }
}

This would be the easiest way if you just want to convert one date. If you need to do this a lot though, it would probably easier to use the second option.

  1. Get the timezone and convert it yourself

With a call to /_api/web/RegionalSettings/TimeZone you can fetch the timezone information of the site:

{
    "d": {
        "__metadata": {
            "id": "<site-url>/_api/Web/RegionalSettings/TimeZone",
            "uri": "<site-url>/_api/Web/RegionalSettings/TimeZone",
            "type": "SP.TimeZone"
        },
        "Description": "(UTC-08:00) Pacific Time (US and Canada)",
        "Id": 13,
        "Information": {
            "__metadata": {
                "type": "SP.TimeZoneInformation"
            },
            "Bias": 480,
            "DaylightBias": -60,
            "StandardBias": 0
        }
    }
}

If you add up all the biases in that response and add those minutes to the dates you get from ListItemAllFields, you get the time in UTC. This way you only need to do one call per site you want to get the metadata from, rather than one call per item.

Even though that doesn't really fix the issue itself of getting non-UTC in the responses (and it's 8 months late 😔), I hope this helps.

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.