Going on a Get-Date…part 2

As mentioned previously in my first part of this Get-Date series, I had run into some issues that were seemingly easy to resolve, yet proved to be a bit more hassle than expected.

Funny enough, the second issue where I ran into date issues was just a day or so apart from the first, so I thought I’d resolve my issues with the knowledge I had obtained previously.

The Problem

I had received an Excel sheet which contained various information, one column being Warranty dates.
Now the issue was that the Excel sheet was generated on a system which had NL-NL Culture configuration, while my machine is running on EN-US configuration.

The reason this is important is because NL-NL has dates sorted as follows:

while EN-US has this configuration:

which means Excel doesn’t recognize the input as dates and formats the cell as general or in some cases even as text.

Take for example 02/07/2012.
NL-NL would say this is 2 July 2012, while EN-US says this is 7 February 2012.

But in the case of for example 24/11/2010 NL-NL would say 24 November 2010 and EN-US would just flip, because that simply can’t be a date format.

The Struggle

When trying to manipulate the data through PowerShell I was running into issues..

Ok, so I needed to convert this to a DateTime object.. let’s give this a shot!

Ah, crap, that was not what I needed…

Let’s use the tips give on my previous issue

Ok, this was not the way to go.. I need to specify which format my date will be in, so that it can be parsed correctly.

So close, yet so far away…

Using my best friend in situations like this, I tracked down the following site which gave excellent examples on how to resolve issues like this.
I first had to define the template format I was using and then parse my input using said template.

Grrrrr, this is becoming annoying!

Browsing more information on the .NET DateTime Parseย and ParseExact Methods provided me with some insight on how it should work, but still I was getting the same error message as before.

It was time to call for help… it had to be something simple, but I was not getting the outcome as expected.

The Solution

How simple can it be… ๐Ÿ™

Again the major issue here is PROPERLY reading what is required.. the answer was right in front of me

Help again came through the FaceBook PowerShell group, and in this case by my local DuPSUG founder and PowerShell MVP Jeff Woutersย :

The template format I had defined earlier was incorrect!

Should be:

Besides that, while the earlier site had mentioned using $null as format provider, but best is to define this properly:

Technical info on [System.Globalization.CultureInfo]::InvariantCulture can be found here, but the best short and readable description I could find was:

The CultureInfo.InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user’s local settings.

Now I can simply display the dates in the correct format and now I see that Excel correctly sees the input as Dates and I can finally sort as wanted!


Happy Scripting! ๐Ÿ™‚



It never hurts to Get-Help

3 thoughts to “Going on a Get-Date…part 2”

  1. Thanks for sharing. Sadly this doesn’t work on my machine, got following error:

    $Date = ’02/07/2012′
    $Template = ‘dd-MM-yyyy’

    [3,1] Exception calling “ParseExact” with “3” argument(s): “De tekenreeks is niet als geldige DateTime herkend.”

    1. Hey GYZ,
      Apologies, that’s what I get for copying part of production code and “re-sampling” it for my site.
      The issue is concerning the template vs date format, which I’ll change in the post.

      $Date = ’02/07/2012′
      is a dd/MM/yyyy format, while I’ve set
      $Template = ‘dd-MM-yyyy’

      I will change accordingly, hopefully this will also resolve the matter for you!

Leave a Reply

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