Table of Contents
Before we start, knowing how dates are stored in PowerShell is essential. Dates are stored as
DateTime
, which contains both a date and a time. The dates are in theMM/DD/YYYY
format. Let’s continue learning different ways to add days to date in PowerShell.
Using AddDays()
Method
Use the AddDays()
method to add days to date.
1 2 3 4 5 6 |
$startDate = Get-Date $endDate = $startDate.AddDays(7) Write-Output "Current Date: $startDate" Write-Output "New Date: $endDate" |
1 2 3 4 |
Current Date: 04/30/2023 14:54:03 New Date: 05/07/2023 14:54:03 |
The above code snippet works in the following way:
- The
Get-Date
cmdlet gets the current date and time and stores it in the$startDate
variable. - The
AddDays()
method of theDateTime
object to add7
days to the$startDate
variable and store the result in the$endDate
variable. - The
Write-Output
cmdlet displays the current date and time stored in the$startDate
variable along with a message"Current Date: "
. - Another
Write-Output
cmdlet displays the new date and time stored in the$endDate
variable.
Using New-TimeSpan
Cmdlet
Use the New-TimeSpan
cmdlet to add days to date.
1 2 3 4 5 6 7 |
$startDate = Get-Date $timeSpan = New-TimeSpan -Days 7 $endDate = $startDate + $timeSpan Write-Output "Current Date: $startDate" Write-Output "New Date: $endDate" |
1 2 3 4 |
Current Date: 04/30/2023 14:57:19 New Date: 05/07/2023 14:57:19 |
The above code fence is similar to the previous code but uses the New-TimeSpan
cmdlet to add days to date.
- The
New-TimeSpan
cmdlet creates a newTimeSpan
object with a specified duration. In this case, the-Days
parameter is used to specify a duration of7
days. This means that the$timeSpan
variable will contain aTimeSpan
object representing a duration of7
days. - Then, we add the
$timeSpan
duration to the$startDate
date using the+
operator, and assigns the result to the$endDate
variable. This creates a newDateTime
object representing the date and time7
days later than the$startDate
date. - Finally, the output is displayed using the
Write-Output
cmdlet.
Further reading:
Using custom Method
We can write our own custom function to add days to date in PowerShell. This method will use AddDays() method to add days to date.
1 2 3 4 5 6 7 8 9 |
function DateAdd([datetime]$date, [int]$days){ return $date.AddDays($days) } $date = Get-Date $newDate = DateAdd -date $date -days 7 Write-Output "Current Date: $date" Write-Output "New Date: $newdate" |
1 2 3 4 |
Current Date: 04/30/2023 15:10:10 New Date: 05/07/2023 15:10:10 |
The above code snippet worked in the following manner:
- The
DateAdd()
function calledDateAdd
takes two parameters: a[datetime]
object called$date
and an[int]
called$days
. The function will add the specified days to the mentioned date and return the new date as a[datetime]
object. - The
AddDays()
method adds the specified number of days ($days
) to the$date
object and returns the resulting new date as a[datetime]
object. - The
Get-Date
retrieves the current date and time and stores it in the$date
variable. - The
$newDate = DateAdd -date $date -days 7
command calls theDateAdd
function with the$date
and7
arguments and stores the resulting new date in the$newDate
variable. - Finally, the resulting output is displayed using the
Write-Output
cmdlet.
In light of the solutions mentioned above, PowerShell provides several ways to add days to a date, making it a powerful feature. These methods include using the AddDays()
method, defining a DateAdd()
function, and using the TimeSpan
object. By leveraging these methods, manipulating dates in PowerShell scripts becomes an effortless task, allowing for the automation of a wide range of functions.
That’s all about PowerShell add days to date.