{"id":102,"date":"2011-11-20T10:29:34","date_gmt":"2011-11-20T17:29:34","guid":{"rendered":"http:\/\/www.jsnover.com\/blog\/?p=102"},"modified":"2013-09-22T18:01:30","modified_gmt":"2013-09-23T01:01:30","slug":"days-till-xmas","status":"publish","type":"post","link":"https:\/\/www.jsnover.com\/blog\/2011\/11\/20\/days-till-xmas\/","title":{"rendered":"Days Till Xmas"},"content":{"rendered":"<p>This morning my daughter asked my wife how many days there were\u00a0until Christmas. That conversation didn&#8217;t go so well.\u00a0 \ud83d\ude42\u00a0\u00a0 Seriously though, this year things started super early.\u00a0 The local store started selling Christmas stuff before Halloween and a couple of weeks ago our neighbors put up their Christmas lights.\u00a0 I can understand why my daughter is getting whipped up into an Christmas frenzy.\u00a0 That said, as my wife contemplates a big day of effort for Thanksgiving, I can understand\u00a0why questions about Christmas are\u00a0not welcome.<!--more--><\/p>\n<p>PowerShell to the rescue (seriously).<\/p>\n<p>This morning&#8217;s drama played out while I was at the computer reviewing <a href=\"http:\/\/dougfinke.com\/blog\/\">Doug Finke&#8217;s<\/a>\u00a0table of contents for a PowerShell for Developers book he is starting to write.\u00a0 (looks like it\u00a0is going to be great!). \u00a0I called my daughter over to explain what a wonderful tool PowerShell was.\u00a0 I explained that if she ever wanted to get\u00a0the date, all she had to do was to type &#8220;Get-Date&#8221;.\u00a0 I then explained how she could cast a string containing a date into a datetime object:\u00a0 [Datetime]&#8221;12\/25\/2011&#8243; .\u00a0 She didn&#8217;t get that one so I quickly moved on to explain that the reason why that mattered is that PowerShell is &#8230; well &#8230; powerful and that it does something called &#8220;object math&#8221;.\u00a0 Object math (the ability to perform mathematical operations on any object which supports them) made everyone&#8217;s life easier because that means you can do things like:<br \/>\n<span style=\"color: #008080;\">PS&gt; [DateTime]<\/span><span style=\"color: #8b0000;\">&#8220;12\/25\/2011&#8221;<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #a9a9a9;\">&#8211;<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #0000ff;\">Get-Date<\/span><span style=\"color: #000000;\">)<span style=\"color: #0000ff;\"><br \/>\n<\/span><span style=\"color: #0000ff;\">Days\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 34<br \/>\nHours\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 13<br \/>\nMinutes\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 21<br \/>\nSeconds\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 12<br \/>\nMilliseconds\u00a0\u00a0\u00a0\u00a0\u00a0 : 588<br \/>\nTicks\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 29856725886382<br \/>\nTotalDays\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 34.556395701831<br \/>\nTotalHours\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 829.353496843944<br \/>\nTotalMinutes\u00a0\u00a0\u00a0\u00a0\u00a0 : 49761.2098106367<br \/>\nTotalSeconds\u00a0\u00a0\u00a0\u00a0\u00a0 : 2985672.5886382<br \/>\nTotalMilliseconds : 2985672588.6382<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><br \/>\nHer face lit up like a Christmas tree!\u00a0 I then remembered this was a repeat of something that happened years ago so I used Bing to search for &#8220;Snover daughter PowerShell Christmas&#8221; and found my <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2006\/04\/25\/583252.aspx\">Days Till Xmas <\/a>post back in 2006 when we still called it Monad.\u00a0 We didn&#8217;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.<\/span><br \/>\n<span style=\"color: #00008b;\">function<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #8a2be2;\">tillXmas<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\"><br \/>\n{<\/span><br \/>\n<span style=\"color: #000000;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500;\">$now<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #a9a9a9;\">=<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #008080;\">[DateTime]<\/span><span style=\"color: #a9a9a9;\">::<\/span><span style=\"color: #000000;\">Now<\/span><span style=\"color: #000000;\"><br \/>\n<\/span><span style=\"color: #008080;\">[Datetime]<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">&#8220;12\/25\/&#8221;<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #a9a9a9;\">+<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #ff4500;\">$now<\/span><span style=\"color: #a9a9a9;\">.<\/span><span style=\"color: #000000;\">Year<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #a9a9a9;\">&#8211;<\/span><span style=\"color: #000000;\">\u00a0<\/span><span style=\"color: #ff4500;\">$Now<\/span><span style=\"color: #000000;\"><br \/>\n}<\/span><\/p>\n<p><span style=\"color: #000000;\">Today I&#8217;ll convert this to a proper function with a proper name and install it on my daughter&#8217;s machine so that she will always know how many seconds until Christmas and won&#8217;t have to ask my wife.\u00a0 <\/span><\/p>\n<p><span style=\"color: #000000;\">PowerShell solves all sorts of problems!\u00a0 \ud83d\ude42<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This morning my daughter asked my wife how many days there were\u00a0until Christmas. That conversation didn&#8217;t go so well.\u00a0 \ud83d\ude42\u00a0\u00a0 Seriously though, this year things started super early.\u00a0 The local store started selling Christmas stuff before Halloween and a couple &hellip; <a href=\"https:\/\/www.jsnover.com\/blog\/2011\/11\/20\/days-till-xmas\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-powershell"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":12,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jsnover.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}