Monthly Archive


PowerShell abolishes Christmas

I was experimenting, OK I was playing around, with dates and decided to put in Christmas Day

PS> $xmas = [datetime]"25/12/09"
Cannot convert value "25/12/09" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:19
+ $xmas = [datetime] <<<< "25/12/09"
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

Oops PowerShell doesn’t allow Christmas!!

Blank look at the screen for a few moments before remembering that we have to use the US format for dates when we are entering them in PowerShell. If I want to enter a date eg 12th March 2009 I would instinctively write

PS> $dwrong = [datetime]"12/3/09"
PS> $dwrong

03 December 2009 00:00:00

And as you can see I would not get what I wanted.  What I need to enter is

PS> $dright = [datetime]"3/12/09"
PS> $dright

12 March 2009 00:00:00

And so Christmas becomes

PS> $xmas = [datetime]"12/25/09"
PS> $xmas

25 December 2009 00:00:00

Living in the UK dates are dd/mm/yyyy not mm/dd/yyyyy so this has been an issue I’ve tripped over a few times.  I had thought that it was a case of PowerShell not having had the bits done to allow other date formats but it turns out it is a design decision.

Lee Holmes has a really good post - – explaining the thinking behind the decision.

Having read it I now understand why I have to perform an un-natural act and put the month first.  Still find it awkward but I least I understand.

Technorati Tags: PowerShell,Date format

Leave a Reply