Convert String to 24 Hour time format in powershell Convert String to 24 Hour time format in powershell powershell powershell

Convert String to 24 Hour time format in powershell


Make sure you convert the retrieved string to a [DateTime] object before trying to format it e.g.:

C:\PS> "{0:HH:mm}" -f [datetime]'12:00 AM'00:00


There will be multiple ways to accomplish this. Here is how I would probably do it:

$test = '0101,8/19/2012,12:00 AM - 12:59 AM,241.83,21'([dateTime]$test.Split(',')[2].Split('-')[0]).ToString('HH:mm')

you can tweak how to get the substring if you want. In a case like this I prefer split over .SubString() since it would be more tolerant of changes in string length.