Convert European date format to US standard

Whenever I get a file from an HR department with a date in there, formatted like “21/12/2012” there could be problems…
When I receive this from HR, it’s in a file… it’s a string.
So when converting this to a datetime format, one could do the following:

Well, this will give you an error, screaming that it’s an impossible date:

This is because of the standard in which dates are formatted. In case of my country, it’s: dd/mm/yyyy.
But in case of the USA, which for Windows systems is the standard, it’s: mm/dd/yyyy
So I’ve written a small function to convert this format to the format the datetime type accelerator accepts 🙂

That means you can use it like so:

4 comments

  1. Pär Björklund says:

    Not sure i’m a fan of using regexps when there’s .NET methods to accomplish the task. I may of course have misunderstood and your intent is to showcase regexps in Powershell. Anyway, something like this should work fine
    [System.DateTime]::Parse(’10/20/2014′,(New-Object system.globalization.cultureinfo ‘en-us’))

    replace date-string and locale with proper values depending on the conversion. I leave it as an excersise to the reader to modify your function to take a date, source locale and output locale and convert to from any supported locales.

  2. Passenger says:

    +1

    [System.DateTime]::Parse(’10/11/2014′,(New-Object system.globalization.cultureinfo ‘fr-fr’))
    #lundi 10 novembre 2014 00:00:00

    [System.DateTime]::Parse(’10/11/2014′,(New-Object system.globalization.cultureinfo ‘en-us’))
    #samedi 11 octobre 2014 00:00:00

    (Convert-DateEuropeToUS ’10/11/2014′) -as [datetime]
    #lundi 10 novembre 2014 00:00:00

    #’10/11/2014′ -as [datetime]
    #lundi 10 novembre 2014 00:00:00

  3. Instead of manually creating a cultureinfo object why not just get the currently used culture and work with that? That way the command will work no matter what the underlying culture is:

    [System.DateTime]::Parse(21/4/2016,([System.Globalization.CultureInfo]::CurrentCulture))

    or:

    [System.DateTime]::Parse(21/4/2016,(Get-Culture))

  4. Sorry, I forgot the quotes around the dates:

    [System.DateTime]::Parse(’21/4/2016′,([System.Globalization.CultureInfo]::CurrentCulture))

    or:

    [System.DateTime]::Parse(’21/4/2016′,(Get-Culture))

Leave a Reply

Your email address will not be published. Required fields are marked *