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/YYYYformat. 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-Datecmdlet gets the current date and time and stores it in the$startDatevariable. - The
AddDays()method of theDateTimeobject to add7days to the$startDatevariable and store the result in the$endDatevariable. - The
Write-Outputcmdlet displays the current date and time stored in the$startDatevariable along with a message"Current Date: ". - Another
Write-Outputcmdlet displays the new date and time stored in the$endDatevariable.
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-TimeSpancmdlet to add days to date.
- The
New-TimeSpancmdlet creates a newTimeSpanobject with a specified duration. In this case, the-Daysparameter is used to specify a duration of7days. This means that the$timeSpanvariable will contain aTimeSpanobject representing a duration of7days. - Then, we add the
$timeSpanduration to the$startDatedate using the+operator, and assigns the result to the$endDatevariable. This creates a newDateTimeobject representing the date and time7days later than the$startDatedate. - Finally, the output is displayed using the
Write-Outputcmdlet.
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 calledDateAddtakes two parameters: a[datetime]object called$dateand 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$dateobject and returns the resulting new date as a[datetime]object. - The
Get-Dateretrieves the current date and time and stores it in the$datevariable. - The
$newDate = DateAdd -date $date -days 7command calls theDateAddfunction with the$dateand7arguments and stores the resulting new date in the$newDatevariable. - Finally, the resulting output is displayed using the
Write-Outputcmdlet.
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.