The ramblings of an old IT Pro travelling the digital byways.

Tuesday, June 28, 2016

Convert string to date and time in PowerShell

Sometimes, the output of a command will include date and time information as a system.string, but you need it in a true datetime format.  Simple solution.

In this case the queueTime property of my $objItem object is a system.string.

$objItem.queueTime

Output:
2016-06-17T13:27:22.3998361Z

Just use the [datetime] accelerator to convert the string to datetime object

[datetime]$objItem.queueTime

Output:
Friday, June 17, 2016 8:27:22 AM


Want to take it a step further?  Want to format that datetime?

"{0:MM/dd/yyyy HH:mm:ss}" -f [datetime]$objItem.queueTime

Output:
06/17/2016 08:27:22

No comments:

About Me

My photo
A living, breathing contradiction.