英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论