Convert string to date time

by mg48 at 2012-10-17 06:37:03

I’m sure this is probably very simple but I haven’t found an answer to it. I have a string that I recieve from WMI - LastBootTime - I need to convert it to a simple date and time. I only need the month, day, year, hour, minute and second so I pull a substring out of the value returned and as an example wind up with this - 20120821182721. I want to convert that string to 8/21/2012 18:27:21. How is that done?
by Makovec at 2012-10-17 07:47:43
Hi,
there are two parts of this problem. First - to convert WMI time to readable format and then format it the way you need. Don’t know what’s your input, but let’s assume it’s something like this:

PS> $wmi = Get-WmiObject Win32_OperatingSystem
PS> $wmi.LastBootUpTime
20121016052029.371107+120

I can convert it to "normal" DateTime:

PS> [System.Management.ManagementDateTimeConverter]::ToDateTime($wmi.LastBootUpTime)
Tuesday, October 16, 2012 05:20:29
PS > $norm = [System.Management.ManagementDateTimeConverter]]
And $norm variable can be converted using Get-Date cmdlet.
PS> Get-Date -Date $norm -Format "M/d/yyyy h]

you can save the result to another variable or file or whatever. If you want to see some other formatting options, you can find it in help of Get-Date cmdlet.

David
by mg48 at 2012-10-18 05:25:00
Thank you David - that is exactly what I needed!