PowerShell Dates and Times
As previously stated, dates and times in PowerShell can be stored in a ‘datetime’ variable. In its simplest form the current date and time can be assigned to a variable as follows.
$exampleDateTime1 = Get-Date [datetime] $exampleDateTime2 = Get-Date
It is also possible to assign a specific date and time to a variable.
$exampleDateTime1 = Get-Date -Date "26/08/2023 12:00:00" [datetime] $exampleDateTime2 = Get-Date -Date "26/08/2023 12:00:00"
If no formatting is applied, a ‘datetime’ variable can be displayed, for example, in the console, in a similar manner to other variables, such as strings, as shown below, using the ‘Write-Host’ cmdlet.
Write-Host $exampleDateTime1
This will display in the console as follows. Note, this format may vary depending on the Operating System regional settings.
26/08/2023 12:00:00
Often, when the value of a ‘datetime’ variable is used, this isn’t an ideal format. PowerShell allows for a number of different formats to be used.
Write-Host "Date: $($exampleDateTime1.ToString('dddd, dd MMMM yyyy'))"
The above example displays the day of the week in words, represented by ‘dddd’, followed by a comma, a two digit day, ‘dd’, the month in words, ‘MMMM’, and finally a four digit year, ‘yyyy’. This is done using the 'ToString' method of the variable itself.
Date: Saturday, 26 August 2023
Similarly, the format of the time portion can be specified.
Write-Host "Time: $($exampleDateTime1.ToString('HH:mm'))"
Here, the time will be displayed with the hours, using a twenty four hour clock, represented by ‘HH’, followed by a colon and then two digits for the minutes, ‘mm’.
Time: 12:00
Below is a table showing format patterns that can be used when displaying dates and times.
Format Pattern |
Description |
---|---|
d | Day of the month as a number from 1 through 31. |
dd | Day of the month as a number from 01 through 31. |
ddd | Three character abbreviation for the day of the week e.g. Mon for Monday. |
dddd | Full name for the day of the week e.g. Monday. |
h | Hours, using a 12 hour clock, with no leading zero e.g. 5. |
hh | Hours, using a 12 hour clock, with a leading zero e.g. 05. |
H | Hours, using a 24 hour clock, with no leading zero e.g. 17. |
HH | Hours, using a 24 hour clock, with a leading zero e.g. 20. |
m | Minutes, with no leading zero e.g. 6. |
mm | Minutes, with a leading zero e.g. 06. |
M | Month as a number, with no leading zero e.g. 3 for March. |
MM | Month as a number, with a leading zero e.g. 03 for March. |
MMM | Three character abbreviation for the month e.g. Mar for March. |
MMMM | Full name for the month e.g. March. |
s | Seconds, with no leading zero e.g. 8. |
ss | Seconds, with a leading zero e.g. 08. |
t | Abbreviation for time of day, AM and PM e.g. A or P. |
tt | Time of day e.g. AM or PM. |
y | Year, without the century and no leading zero e.g. 9. |
yy | Year, without the century and a leading zero e.g. 09. |
yyyy | Year, including the century e.g. 2017. |
These format patterns can also be used with the 'Get-Date' cmdlet, to format the current date and/or time.
Write-Host "Today's date is: $(Get-Date -Format 'dd/MM/yyyy')."
As well as being able to format dates and times using the format patterns above, PowerShell also provides a number of properties to access individual components of a date and time, as shown below.
$exampleDateTime1 = Get-Date -Date "26/08/2023 12:00:00" Write-Host $exampleDateTime1.Day Write-Host $exampleDateTime1.DayOfWeek Write-Host $exampleDateTime1.Month Write-Host $exampleDateTime1.Year Write-Host $exampleDateTime1.Hour Write-Host $exampleDateTime1.Minute Write-Host $exampleDateTime1.Second
On top of these properties, there are also built in methods for manipulating dates and times, as can be seen below, where one is added to each component of a date and time.
$exampleDateTime1 = Get-Date -Date "26/08/2023 12:00:00" Write-Host $exampleDateTime1 $exampleDateTime1 = $exampleDateTime1.AddDays(1) $exampleDateTime1 = $exampleDateTime1.AddMonths(1) $exampleDateTime1 = $exampleDateTime1.AddYears(1) $exampleDateTime1 = $exampleDateTime1.AddHours(1) $exampleDateTime1 = $exampleDateTime1.AddMinutes(1) $exampleDateTime1 = $exampleDateTime1.AddSeconds(1) Write-Host $exampleDateTime1
The above will display the original date and time, followed by the date and time with one added to each component.
26/08/2023 12:00:00 27/09/2024 13:01:01
In order to subtract from the various components, a negative value can be used with the above methods, for example minus one to subtract one day.
The properties and methods discussed above are by no means exhaustive. There are many others available.