Using Powershell For Dates, Months, Days and More

No matter what Database you deal with there will be times where you need to manipulate or calculate dates and Powershell offers many different methods to get them. Whether you’re going back in time to 90 days ago or you need to set a variable for a future time and add a day Powershell provides solutions.

We couldn’t possibly cover all of the functions with examples here so I am just including common commands and some that I have run into or found very useful.

Powershell and Dates | Get-Date

Sometimes I just need to get the current date or sometimes I need today’s date and the ability to grab things like the week, month, day or time from it. The result is a very human readable result along with the name of the Month and Day too.

Get-Date  ##  Monday, May 31, 2055 11:09:00 AM

We can also pass a date as a string to the function and get the same info:

Get-Date -Date '2055-02-13'  ## Saturday, February 13, 2055 12:00:00 AM
Get-Date -Date '2055-01-12 10:09:03 AM'  ## Now we have the time element

Single Values

Instead of using the Select-Object commandlet you can also use the parenthesis () and grab just the value your looking for from Get-Date. If we pass a string date we can use the parenthesis the same way and grab the single values.

This has been very useful when building a Datatable as we can separate the fields (month, day, year) and search or sort based on them instead of always using the date objects.

(Get-Date '2055-02-13 11:15:41 AM').DayOfWeek
(Get-Date '2055-02-13 11:15:41 AM').Month

Now that we can read the date and it’s elements… Let’s pull the elements out of this Commandlet so we can use them in variables and other statements.

Get the Year and Day of the Year

Get-Date | Select-Object Year   ##  2055
Get-Date '2055-08-13 11:15:41 AM' | Select-Object DayofYear   ##  225
(Get-Date).Year   ##  2055

Get Day of the Week Number or Name

Get-Date '2055-02-13 11:15:41 AM' | Select-Object Day  ##  13
Get-Date | Select-Object DayofWeek  ## Saturday
Get-Date '2055-02-13 11:15:41 AM' | Select-Object Month  ##  2
(Get-Date '2055-02-13 11:15:41 AM').DayOfWeek   ##  Saturday

Get Last Day of Month

One of my own solutions was for finding the last day of the month by using the current date or a provided date. I didn’t find a commandlet that solves this easily. In this example, I simply grab the month that is “ahead” of the one provided and do some math by subtracting the days. My previous method was to change the date to the first of the “next” month and subtract 1 day but this is shorter and it works, somehow 🙂

$fulldate = [Datetime]'2052-01-30'  ## Cast our date as a Datetime object
$fulldate = $fulldate.AddMonths(1)  ## $fulldate becomes Feb. 29th, ??
$fulldate.AddDays(-$fulldate.Day )   ## Subtract the days to get the last day

I purposely used the Jan. 30th date because it is a leap year. I am casting the string date to a Datetime which is also setting the date to a default of midnight. If you have a time on it this may not work. Then I add 1 month to the date.

At this point point the value of $fulldate is ‘2/29/2052’,..

But when we subtract those days to get the end of the previous month, it is correct. I think the Commandlet is doing the “leap year” work for us in that we asked to add a Month to the date given so it did exactly that. Under the hood the date becomes “Feb. 29th” but the system knows there is no 29.

If you check the value of $fulldate and change the day (January) from 31 to 30 to 29 and 28 you will see that the script will return the Feb. 29th date each time until you get to the 28th.

Add Days to Today’s Date

We can use the plus sign to add days (+) to a date or minus (-) to subtract them. We can also convert the Datetime format that get-date creates to just a date with Month, Day and Year. This sets the time to midnight.

(get-date).AddDays(+5)   ## add 5 days
(get-date).AddDays(+120)   ## add 120 days
(get-date).AddDays(+90).Date   ## Sets the time to midnight

Get Date From 90 Days Ago

(get-date).AddDays(-90)   ## subtract 90 days

$date = [Datetime]'2043-06-18'  ## string date

Get Date and Add Hours

(get-date).AddHours(+5)   ## add 5 hours
(get-date).AddHours(-5)   ## subtract 90 hours
$date = [Datetime]'2043-04-23'.AddHours(+5)  ## string date

Get and Set a Timezone

Another common requirement is to either collect or define the timezone so dates and and times are correct when saving data. You will notice the default for Get-Date is the system Time=Zone, I tend to save all time records under the UTC timezone so I may need to change this.

Get-Timezone  ##  system timezone Id, Name and offset from UTC time
Get-Timezone -ListAvailable  ##  All of the Timezones that Powershell knows about
Get-Timezone 'Central Standard Time'  ## Get the CST time
Get-Timezone 'Pakistan Standard Time'  ## Get Pakistan Timezone

Using the -ListAvailable switch we can get another timezone and set it as the system default. To Set the timezone for the system:

Get-Timezone -ListAvailable   ## get the list of timezones

Set-TimeZone -Id "Mountain Standard Time"   ## use the Id to set it