Ezine 193 - Get-Date How Many Days to Christmas?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 SecretNormally 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 VariablesIn 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 $Today =get-date $Xmas = ($Christmas.DayOfYear - $Today.DayOfYear) 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. Example 2 - Production Version of 'How Many Days to Christmas'"There are " + (([system.datetime]"25 december 2009").DayOfYear -
` 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 ResearchI'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 #Also try 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 ParseExactOnce 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. Summary of PowerShell Get-Time and DateTimeScripting 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. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever See more Microsoft PowerShell tutorials• Windows PowerShell Home • Introduction • Cmdlets • Exchange 2007 • Profile.ps1 • $_.Pipeline If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit. *
| |||||