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?
You can use the ParseExact method:
[datetime]::ParseExact('24122014_022257','ddMMyyyy_HHmmss',$null)
Wednesday, December 24, 2014 2:22:57 AM
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)
get-date 24122014022257
Wednesday, December 24, 2014 2:22:57 AM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With