Monthly Archive

Categories

Playing with dates

I happened to notice that Sundays date was 5 July 2009.  Not particular astute you may think but I noticed it because I’d written it as 5/7/9.  English date format puts the day then the month so this may not make sense in other formats.

The fact that there was a difference to two between the day and the month and the same between the month and the year intrigued me.  It doesn’t take much. So I decided to look at other dates that follow that pattern

001
002
003
004
for ($i=3; $i -le 12; $i++){
    $date = Get-Date -Month $i -Day $($i -2) -Year $(2000 + $i +2)
    "{0} {1}" -f $date.DayOfWeek, $date.ToShortDateString()   
}

 

It doesn’t need much to do this.  A for loop – notice we start at 3 so we don’t get into negative days. Time travel by PowerShell now there’s a thought

Get-Date | Set-Now –Era Jurassic

gets us back to the dinosaurs.  OK I’ll stop.

We can use Get-Date and give it a month, day and year to create the date.  Note that I add 2000 to get the current sequence of dates. This sequence will happen every century.  One thing I noticed was that Get-Date takes the year you give it literally.  It doesn’t make any allowance for the century if you don’t supply the full year.  Compare

PS> Get-Date -Day 1 -Month 1 -Year 9

01 January 0009 19:46:48

PS> [datetime]"1/1/9"

01 January 2009 00:00:00

Finally I use a formatted string to display the date (in short form) and the day of the week.  The day of week doesn’t appear to add any more information but I was curious.

Technorati Tags: PowerShell,Dates

Leave a Reply