Search code examples
datepowershelldatetimedate-conversion

Powershell Get-Date altered to another zone


I'm constructing a script to read dates from files, and I've been getting a strange problem after reading the EXIF 'Date Taken' data.

The ultimate purpose is to rename directories of images using the EXIF date.

So, the problem:

I have a variable in DateTime format:

PS > $oldtime
25 May 2015 16:44:07

I convert the date using Get-Date to another format:

$newtime = $oldTime | Get-Date -Format yyyyMMdd_hhmmss

I get this output:

PS > $newtime
20150525_044407

I am assuming that the date is being altered to cater for Time Zone or something, but I am confused why, as I am trying to specify and extract the date.

What I was expecting is:

PS > $newtime
20150525_164407

Does anyone know how to get around this?


Solution

  • In your date format string, use HH for 24-hour time. hh gives you 12-hour (AM/PM).

    $oldTime | Get-Date -Format yyyyMMdd_HHmmss;