PowerShell's Get-Date and DateTimeWindows PowerShell's Get-Date and DateTimeManipulating dates is always tricky. However, PowerShell's get-Date cmdlet does a wonderful job of interpreting the different international formats for day / month / year. As a result, what every your computer's country locale, it will be easy to calculate how many days there are to Christmas. Topics for Windows PowerShell Get-Date and DateTime
♣ Introduction to the Get-Date CmdletNormally I like to use a learning progression where we begin with get-Help and examine a cmdlet's parameters, next we look at the properties by piping the cmdlet into get-Member. Finally, we use the knowledge gained to create PowerShell script to tackle a real-life task. On this page I am making a change to my usual running order. Firstly I want to tackle the challenge presented by a real-life task first. Secondly, I will encourage you to research more methods and properties for the cmdlet get-date. The result is that you have the skills to undertake other DateTime projects. 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 until Christmas, Thanksgiving, or any other date you care to place in a text string. Here in the UK, our operating systems show dates in the format: 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 values that it could understand and perform calculations. Example 1 - Calculate Days to Christmas Using Lots of VariablesIn example one I have used several variables that are not strictly necesary, 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, 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 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.
Guy Recommends: SolarWinds LANSurveyor
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |