Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to DateTime conversion in PowerShell

I am trying to convert the time stamp value of a file to date time for comparing two date time differences.

The data would be coming in format 24122014_022257. I need to convert this into date time so that I can compare the values:

$usdate="24122014_022257"   
$dateParts = $usdate -split "_"   
$final = $dateparts[0] + $dateParts[1]   

How can I do it?

like image 868
Renji Avatar asked Jan 02 '15 11:01

Renji


3 Answers

You can use the ParseExact method:

[datetime]::ParseExact('24122014_022257','ddMMyyyy_HHmmss',$null)

Wednesday, December 24, 2014 2:22:57 AM
like image 197
mjolinor Avatar answered Sep 19 '22 11:09

mjolinor


ParseExact will do just that. You can specify custom datetime format in second parameter. You can leave third parameter as null, or specify a culture to use (it may matter if you're importing files that were generated from system with different time zone).

$usdate="24122014_022257"
[datetime]::ParseExact($usdate,"ddMMyyyy_HHmmss", [System.Globalization.CultureInfo]::CurrentCulture)
like image 36
AdamL Avatar answered Sep 19 '22 11:09

AdamL


get-date 24122014022257

Wednesday, December 24, 2014 2:22:57 AM

like image 23
Abhishek Singha Avatar answered Sep 20 '22 11:09

Abhishek Singha