Solved

powershell datetime parse help

Posted on 2014-09-22
6
271 Views
Last Modified: 2014-09-24
Hello EE,

I have a a creation date in string format from gwmi win32_process , it returns a date format that is unusual , how do I parse this correctly, as the nano seconds after the seconds does not have a character format that I am aware of ?

20140913014133.145231+000

 {[datetime]::ParseExact($_.CreationDate,????? ,$null)}
0
Comment
Question by:davesnb
6 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 400 total points
ID: 40336752
Does this work for you?

[DateTime]::ParseExact($_.CreationDate.substring(0,14),'yyyyMMddhhmmss',$null)
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 100 total points
ID: 40336852
You can also just split the date (though the substring should work as well)

 [DateTime]::ParseExact(($_creationDate.split(".")[0]), 'yyyyMMddHHmmss', $null)

Open in new window

0
 

Author Closing Comment

by:davesnb
ID: 40336874
Works great, thank you
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:Dale Harris
ID: 40336881
You need to be careful not to use the HH since it needs to be in 12 hour format, not 24 in order for it to properly parse.  But yes, either way whether you split it or substring it should work just fine.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40336909
Usually that is a sufficient way to convert WMI Dates, but the "officially correct" way is to use this:
[ManagementDateTimeConverter]::ToDateTime($_.creationDate)

Open in new window

This allows for adjustment of time zone info, which can get important if you use a remote WMI query.
0
 
LVL 40

Expert Comment

by:footech
ID: 40341197
I know the question is already answered, but was just going through my email alerts.
The other way that I've always seen is to use the ConvertToDateTime method that is added to all WMI objects returned by PowerShell.  Quick example.
$a = gwmi win32_process -filter "name = 'svchost.exe'"
$a | % { $_.ConvertToDateTime($_.CreationDate) }

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question