PowerShell’s Get-Date Calculates ‘How Many Days to Christmas’
Manipulating dates is always tricky. However, PowerShell’s get-Date cmdlet does a wonderful job of coping with the different international formats for day / month / year. As a result, what every your country, it will be easy to calculate how many days there are to Christmas.
Topics for PowerShell Get-Date and DateTime
- This Week’s Secret
- How Many Days to Christmas? (or Thanksgiving)
- Example 1 – Calculate Days to Christmas Using Lots of Variables
- Example 2 – Production Version of ‘How Many Days to Christmas’
- Bonus Technique – DateTime and ParseExact
♣
This Week’s Secret
Normally I like to use a learning progression where we begin by examining a cmdlet’s parameters with get-Help, and then we look at the properties by piping the cmdlet into get-Member. Finally, we use this knowledge to create a real-life script.
This week I am making a change to my usual running order. Firstly I want to tackle the challenge presented by the real-life task first. Secondly, I will encourage you to research more methods and properties for the cmdlet get-date, thereby encouraging you to tackle other projects.
One more request, I am collecting handy PowerShell scripts, thus do send me your real-life scripts in general and datetime scripts in particular.
How Many Days to Christmas? (or Thanksgiving)
Our challenge is simple enough, to knock-up a simple script which will tell us how many days there are to Christmas, Thanksgiving or any other date you care to place in a string.
Here in the UK, our operating system shows dates as dd mmmm yyyy. Whereas in the United States your locale is displayed as mmmm dd yyyy. Consequently, I was amazed that PowerShell could convert both "25 December 2009" and "November 26 2009" into date formats that it could understand.
Example 1 – Calculate Days to Christmas Using Lots of Variables
In this example I have used several non-essential variables, I just wanted to show my thought process in creating this very simple PowerShell script.
# PowerShell Script to display days until Christmas
$DecDate = "25 December 2009"
$NovDate ="November 26 2009"
$Thanksgiving = [system.datetime]$NovDate
$Christmas = [system.datetime]$DecDate
$Today =get-date
$Xmas = ($Christmas.DayOfYear – $Today.DayOfYear)
$Thanks = ($Thanksgiving.DayOfYear – $Today.DayOfYear)
"There are " + $Xmas + " days until " + $DecDate
"There are " + $Thanks + " days until " + $NovDate
Note 1: You may wish to examine the values for $DecDate and $NovDate and then change the sequence of day month to suit your culture.
Note 2: It’s interesting to see how PowerShell leverages .Net Framework, for example, it employs System.DateTime to convert a text string to a date format.
Note 3: I keep marvelling that PowerShell could understand both formats dd mmmm yyyy and mmmm dd yyyy.
Guy Recommends: WMI Monitor and It’s Free!
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Example 2 – Production Version of ‘How Many Days to Christmas’
"There are " + (([system.datetime]"25 december 2009").DayOfYear – `
(get-date).DayOfYear) + " days until " + "25 December 2009"
Note 1: This is just one long command. Observe how the backtick (`) enables the command to overspill onto the second line.
Note 2: This command needed an extra set of round brackets to surround [system.datetime]"25 December 2009"
Note 3: It’s interesting to see how PowerShell interprets a mixture of "text strings" and date calculations. As with types of bracket, you need the correct type of "double" speech marks here.
Further Research
I’m hoping that the simple example above will give you ideas for date scripts, which will be useful in your PowerShell projects. If so, then it’s well worth examining get-Date’s properties, and in particular its methods.
# Research PowerShell’s get-Date
get-Date | get-Member
#Also try
get-Help get-Date -full
Note 1: Normally it’s a cmdlet’s properties that I am most interested in, but with get-date it’s the methods that intrigue me, for example addDays and IsDaylightSavingTime.
Note 2: As I mentioned earlier, I am collecting real-life PowerShell scripts, thus do send me your datetime scripts. If it’s really simple then that will help newbies, if it’s more sophisticated than mine, then that will help people with a similar task.
Bonus Technique – DateTime and ParseExact
Once again, here is PowerShell working with the .Net Framework class DateTime. ParseExact is a neat method which can convert a text string into its DateTime equivalent.
# ParseExact Change date and time with custom specifier.
$DateString = "Sun 04 Oct 2009 12:30 AM -06:00"
$Format = "ddd dd MMM yyyy h:mm tt zzz"
$Translate = [DateTime]::ParseExact($DateString, $Format, $provider)
Write-Host "$DateString converts to $($result.ToString())."
Summary of PowerShell Get-Time and DateTime
Scripting dates is always tricky. In these examples we can see how PowerShell leverages .Net Framework to convert strings into DateTime values, which it can then use for calculations.
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
If you like this page then please share it with your friends
See more PowerShell examples for syntax advice
• PowerShell Syntax • PowerShell Array • PowerShell Array String Conversion • Plist
• Get-Date • PowerShell Quotes • PowerShell variables • RegEx • PowerShell functions
• PowerShell Tutorials • Conditional operators • PowerShell Hashtable • Windows PowerShell
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.