SOLVED

Convert a text string to a correct date time format

Brass 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 (Brass 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
1 best response

Accepted Solutions
best response confirmed by Andre Radtke (Brass 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

View solution in original post