Days Till Xmas

This morning my daughter asked my wife how many days there were until Christmas. That conversation didn’t go so well.  🙂   Seriously though, this year things started super early.  The local store started selling Christmas stuff before Halloween and a couple of weeks ago our neighbors put up their Christmas lights.  I can understand why my daughter is getting whipped up into an Christmas frenzy.  That said, as my wife contemplates a big day of effort for Thanksgiving, I can understand why questions about Christmas are not welcome.

PowerShell to the rescue (seriously).

This morning’s drama played out while I was at the computer reviewing Doug Finke’s table of contents for a PowerShell for Developers book he is starting to write.  (looks like it is going to be great!).  I called my daughter over to explain what a wonderful tool PowerShell was.  I explained that if she ever wanted to get the date, all she had to do was to type “Get-Date”.  I then explained how she could cast a string containing a date into a datetime object:  [Datetime]”12/25/2011″ .  She didn’t get that one so I quickly moved on to explain that the reason why that mattered is that PowerShell is … well … powerful and that it does something called “object math”.  Object math (the ability to perform mathematical operations on any object which supports them) made everyone’s life easier because that means you can do things like:
PS> [DateTime]“12/25/2011”  (Get-Date)
Days              : 34
Hours             : 13
Minutes           : 21
Seconds           : 12
Milliseconds      : 588
Ticks             : 29856725886382
TotalDays         : 34.556395701831
TotalHours        : 829.353496843944
TotalMinutes      : 49761.2098106367
TotalSeconds      : 2985672.5886382
TotalMilliseconds : 2985672588.6382

Her face lit up like a Christmas tree!  I then remembered this was a repeat of something that happened years ago so I used Bing to search for “Snover daughter PowerShell Christmas” and found my Days Till Xmas post back in 2006 when we still called it Monad.  We didn’t have the Get-Date Cmdlet back then so I used [DateTime]::Now but notice that this script uses the YEAR property and string math to always calculate the days till this years Christmas.

function tillXmas ()

    $now = [DateTime]::Now
[Datetime](“12/25/” + $now.Year)  $Now

Today I’ll convert this to a proper function with a proper name and install it on my daughter’s machine so that she will always know how many seconds until Christmas and won’t have to ask my wife. 

PowerShell solves all sorts of problems!  🙂

3 thoughts on “Days Till Xmas

  1. Pingback: I Remember Those Days (Until Christmas) | The Mosquito Eater

  2. Pingback: PowerShell: date arithmetic | The Things I do

Leave a Reply

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