Martinecko
Martinecko

Reputation: 1929

converting date from given string

I need to identify this string containing Date

20151110171937.050162+060

It's CreationDate property of object Get-WmiObject -Class win32_process. I need to use it as input to New-TimeSpan cmdlet. Therefore, it should be valid [DateTime] object, but I don't know how to convert it.

Upvotes: 2

Views: 1341

Answers (3)

Markus
Markus

Reputation: 438

The string "20151110171937.050162+060" is a WMI datetime format and is a equivalent to CIM_DATETIME datatype with following format (yyyymmddHHMMSS.mmmmmmsUUU) where;

  • mmmmmm Six-digit number of microseconds in the second (000000 through 999999). Your implementation does not have to support evaluation using this field. However, this field must always be present to preserve the fixed-length nature of the string.

  • s Plus sign (+) or minus sign (-) to indicate a positive or negative offset from Coordinated Universal Times (UTC).

  • UUU Three-digit offset indicating the number of minutes that the originating time zone deviates from UTC. For WMI, it is encouraged, but not required, to convert times to GMT (a UTC offset of zero).

The UUU is of significance since it does change for you local time zone. You can test this by changing your local time zone and running for example

(Get-WmiObject Win32_OperatingSystem).InstallDate

You can covert using PeterSerAl mention or by this shortcut for a one off

([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)

But note there's a bug with ConvertToDateTime() and [System.Management.ManagementDateTimeConverter]::ToDateTime() in Powershell, it does not respect local time zone and will give you the wrong date. Analysis here, but this short and long of it that you have to reboot the Powershell shell or ISE for this to work.

Upvotes: 0

user4003407
user4003407

Reputation: 22102

PowerShell extend each WMI object with two methods ConvertFromDateTime and ConvertToDateTime which allows you to convert date and time between WMI string and .NET [DateTime] representations. This methods actually are just references to [System.Management.ManagementDateTimeConverter] static methods ToDmtfDateTime and ToDateTime.

Upvotes: 4

durbnpoisn
durbnpoisn

Reputation: 4659

The part before the decimal is "20151110171937"

Or 2015-11-10 17:19:37

There's your date/time stamp. Ignore the rest.

Upvotes: 0

Related Questions