Date and Time format with ParseExact using Powershell
-
06-02-2021 - |
Domanda
I have a single ligne column (Called : Next Due Diligence Date) in my sharepoint list where i have a date with "MM-YYYY" format. I want to parse it to "01/MM/YYYY"
For this, I tried the Code below :
$dateString =$IM["Next Due Diligence Date"]
$format ="01/MM/YYYY"
$start = [datetime]::parseexact($dateString, $format, $null)
I have this Error : Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
Soluzione
If you are sure that $IM["Next Due Diligence Date"]
contains value in "MM-YYYY"
format, you could do something like this:
$dateString =$IM["Next Due Diligence Date"]
$dateStringParts = $dateString.Split('-');
$start = New-Object DateTime([Int32]::Parse($dateStringParts[1]), [Int32]::Parse($dateStringParts[0]), 1)
and after that you could format the date with this:
$start.ToString("dd/MM/yyyy")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange