Search code examples
powershellsharepointsharepoint-2010odatapowershell-3.0

SharePoint list date/time field set via REST API converted to universal time


I'd like to set a date/time field to a value at midnight:

# Tuesday, May 19, 2015 12:00:00 AM
$Properties = @{CompletedDate = [DateTime]::Today}
...

$Payload = $Properties | ConvertTo-Json

$headers = @{
  "X-HTTP-Method" = "MERGE";
  "If-Match" = "*"
}

Invoke-WebRequest -Uri $url -Method Post -UseDefaultCredentials -Headers $headers -ContentType "application/json" -Body $Payload

It appears, however, that the date is being adjusted to Universal Time:

<entry>
  <content>
    <m:properties>
      ...
      <d:CompletedDate m:type="Edm.DateTime">2015-05-19T04:00:00</d:CompletedDate>

If I want CompletedDate to actually 2015-05-19T00:00:00, what's the best way to ensure this?

I suppose one option is:

$today = Get-Date -Date ('{0}/{1}/{2} 00:00:00Z' -f (Get-Date).year, (Get-Date).month, (Get-Date).day)

$Properties = @{CompletedDate = $today}

Is there a more-efficient way?


Solution

  • Since SharePoint expects date and time values to be specified in local time, the following example shows how to save 2015-05-19T00:00:00 value:

    $completedDate = Get-Date -Date "2015-05-19T00:00:00" #UTC
    $completedDate = $completedDate.ToLocalTime()   
    
    $ItemProperties = @{  
           Title = "Approval Task"; 
           CompletedDate = $completedDate}