如何在PowerShell中转换字符串日期?

huangapple go评论101阅读模式
英文:

How to convert string dates in PowerShell?

问题

SharePoint列表使用的常见日期/时间格式是2020-01-06T08:09:00.0000000Z。我有一个源日期,我要写入该列表,其格式不同,即11.07.2012 09:40

如何将该字符串转换为SharePoint列表期望的格式?

我最接近的尝试是:

[datetime]$dateToConvert = "11.07.2012 09:40"
Get-Date -Date $dateToConvert -Format FileDateTimeUniversal

它输出20121107T0840000000Z,但缺少破折号。

有什么想法吗?

英文:

I'm working with a SharePoint list and it's common date/time format is
2020-01-06T08:09:00.0000000Z. I have source date that I'm writing to that list that's in another format, i.e. 11.07.2012 09:40.

How can I convert that string into the format that the SharePoint list expects?

Closest I've come is:

[datetime]$dateToConvert = "11.07.2012 09:40"
Get-Date -Date $dateToConvert -Format FileDateTimeUniversal

which outputs 20121107T0840000000Z but that's missing the dashes.

Any ideas?

答案1

得分: 2

你可以尝试这样做:$dateToConvert.ToUniversalTime().ToString("o")

  • 使用ToUniversalTime()以获取末尾的"Z"(而不是UTC偏移)
  • 使用ToString("o")以获取符合ISO 8601的字符串
英文:

You can try this: $dateToConvert.ToUniversalTime().ToString("o")

  • ToUniversalTime() to get the "Z" at the end (instead of the UTC offset)
  • ToString("o") to get a string that complies with ISO 8601

huangapple
  • 本文由 发表于 2020年1月6日 17:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定