colonel_claypoo
colonel_claypoo

Reputation: 615

How to convert string dates in PowerShell?

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?

Upvotes: 1

Views: 863

Answers (1)

Alexis Mathieu
Alexis Mathieu

Reputation: 692

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

Upvotes: 2

Related Questions