SOLVED

Convert a text string to a correct date time format

%3CLINGO-SUB%20id%3D%22lingo-sub-3072617%22%20slang%3D%22en-US%22%3EConvert%20a%20text%20string%20to%20a%20correct%20date%20time%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3072617%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20folks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20maybe%20a%20simple%20question%20for%20the%20experienced%20people%20around%20here%20%3A)%3C%2Fimg%3E%20.%20I%20have%20a%20runbook%20in%20azure%20that%20is%20getting%20the%20field%20%22CreatedDateTime%22%26nbsp%3Bof%20a%20unified%20group%20(Teams%20Team).%20The%20format%20of%20this%20field%20ist%20like%20the%20following%20%222020-08-06T06%3A03%3A19Z%22.%20This%20is%20not%20recognized%20as%20a%20valid%20DateTime%20format%20by%20the%20cmdlet%3A%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%24DateTime%3C%2FSPAN%3E%3CSPAN%3E%3D%5BDatetime%5D%3A%3AParseExact(%3C%2FSPAN%3E%3CSPAN%3E%24DateTime%3C%2FSPAN%3E%3CSPAN%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E'MM%2Fdd%2Fyyyy'%3C%2FSPAN%3E%3CSPAN%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%24null%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3ECan%20anyone%20point%20me%20to%20a%20solution%20how%20to%20get%20the%20string%20%22%3CSTRONG%3E2020-08-06T06%3A03%3A19Z%3C%2FSTRONG%3E%22%20into%20a%20string%20like%20%22%3CSTRONG%3E2020-08-06%3C%2FSTRONG%3E%22%20or%26nbsp%3B%22%3CSTRONG%3E06-08-2020%3C%2FSTRONG%3E%22.%20Every%20hint%20is%20appreciated.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EThank%20you%20all%20%3B)%3C%2Fimg%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3072617%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Automation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3072664%22%20slang%3D%22en-US%22%3ERe%3A%20Convert%20a%20text%20string%20to%20a%20correct%20date%20time%20format%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3072664%22%20slang%3D%22en-US%22%3EFor%20your%20information%2C%20I%20am%20getting%20the%20CreatedDateTime%20format%20from%20the%20MS%20Graph%20as%20you%20can%20read%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fde-de%2Fgraph%2Fapi%2Fgroup-list%3Fview%3Dgraph-rest-1.0%26amp%3Btabs%3Dhttp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fde-de%2Fgraph%2Fapi%2Fgroup-list%3Fview%3Dgraph-rest-1.0%26amp%3Btabs%3Dhttp%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EI%20am%20wondering%20why%20Microsoft%20is%20formatting%20this%20field%20in%20this%20wierd%20format%20that%20I%20can%20not%20use%20for%20further%20actions.....%20seriously%20microsoft%3F!%3F%20%F0%9F%A4%B7%E2%80%8D%3Amale_sign%3A%3C%2FLINGO-BODY%3E
Contributor

Hello folks,

 

I have maybe a simple question for the experienced people around here :) . I have a runbook in azure that is getting the field "CreatedDateTime" of a unified group (Teams Team). The format of this field ist like the following "2020-08-06T06:03:19Z". This is not recognized as a valid DateTime format by the cmdlet:

$DateTime=[Datetime]::ParseExact($DateTime'MM/dd/yyyy'$null)
 
Can anyone point me to a solution how to get the string "2020-08-06T06:03:19Z" into a string like "2020-08-06" or "06-08-2020". Every hint is appreciated.
 
Thank you all ;)
4 Replies
For your information, I am getting the CreatedDateTime format from the MS Graph as you can read here: https://docs.microsoft.com/de-de/graph/api/group-list?view=graph-rest-1.0&tabs=http

I am wondering why Microsoft is formatting this field in this wierd format that I can not use for further actions..... seriously microsoft?!? 🤷‍:male_sign:

Look like Get-Date will return a string that can be converted to a date:

 

[datetime](get-date('2020-08-06T06:03:19Z')) | get-member -MemberType Properties

[Edit] Actually just noticed the flaw with your script sample - you need to provide the date format that the incoming string is in so that ParseExact is receiving

$DateTime=[Datetime]::ParseExact($str, 'yyyy-MM-ddTHH:mm:ssZ', $null)

 

@Jonathan_Allen 

thank you for your reply, but it does not help me at the moment because all it gives me back is the following:

PS C:\WINDOWS\system32> [datetime](get-date('2020-08-06T06:03:19Z')) | get-member -MemberType Properties


TypeName: System.DateTime

Name MemberType Definition
---- ---------- ----------
Date Property datetime Date {get;}
Day Property int Day {get;}
DayOfWeek Property System.DayOfWeek DayOfWeek {get;}
DayOfYear Property int DayOfYear {get;}
Hour Property int Hour {get;}
Kind Property System.DateTimeKind Kind {get;}
Millisecond Property int Millisecond {get;}
Minute Property int Minute {get;}
Month Property int Month {get;}
Second Property int Second {get;}
Ticks Property long Ticks {get;}
TimeOfDay Property timespan TimeOfDay {get;}
Year Property int Year {get;}
DateTime ScriptProperty System.Object DateTime {get=if ((& { Set-StrictMode -Ver...

 

What I needed was a correct format as described. I solved it my way by cutting the last characters with this: 

$CreatedDate.Remove(10,10)

it´s not smart but for now it will be enough for me ;)

best response confirmed by Andre Radtke (Contributor)
Solution
sorry, I left the " | Get-Member " to show you that the type returned is a Date type. remove that to integrate into your own script