Search code examples
powershellpowershell-4.0

What date format is this : Mon Oct 18 10:26:10 EDT 2021"


What date format is the below and how to get this converted to check with current date in Powershell

Mon Oct 18 10:26:10 EDT 2021


Solution

  • EDT means Eastern Daylight Time (North America), which is 4 hours behind from the UTC universal time.

    Using that, you could parse it to your local time by first adding the 4 hours offset to get the date in UTC, and finally using method .ToLocaltime() on it.

    [datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4).ToLocaltime()
    

    On my (Dutch) machine it doesn't seem to matter, but if you want to make absolutely sure the parsed-out date is regarded as being UTC (instead of its .Kind being set to 'Unspecified', use this:

    [datetime]::SpecifyKind([datetime]::ParseExact('Mon Oct 18 10:26:10 EDT 2021', 'ddd MMM dd HH:mm:ss \E\D\T yyyy', [cultureinfo]'en-US').AddHours(4), 'UTC').ToLocaltime()