通过REST API设置的SharePoint列表日期/时间字段转换为通用时间

时间:2015-05-19 16:24:13

标签: powershell sharepoint sharepoint-2010 odata powershell-v3.0

我想将日期/时间字段设置为午夜的值:

# 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

然而,似乎日期正在调整为世界时:

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

如果我希望CompletedDate实际上是2015-05-19T00:00:00,那么确保这一点的最佳方式是什么?

我想一个选项是:

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

$Properties = @{CompletedDate = $today}

有更有效的方法吗?

1 个答案:

答案 0 :(得分:2)

由于SharePoint期望在本地时间指定日期和时间值,因此以下示例显示如何保存2015-05-19T00:00:00值:

$completedDate = Get-Date -Date "2015-05-19T00:00:00" #UTC
$completedDate = $completedDate.ToLocalTime()   

$ItemProperties = @{  
       Title = "Approval Task"; 
       CompletedDate = $completedDate}