PowerShell Add Days to Date

PowerShell add days to Date

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 the MM/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.

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 the DateTime object to add 7 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.

The above code fence is similar to the previous code but uses the New-TimeSpancmdlet to add days to date.

  • The New-TimeSpan cmdlet creates a new TimeSpan object with a specified duration. In this case, the -Days parameter is used to specify a duration of 7 days. This means that the $timeSpan variable will contain a TimeSpan object representing a duration of 7 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 new DateTime object representing the date and time 7 days later than the $startDate date.
  • Finally, the output is displayed using the Write-Output cmdlet.

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.

The above code snippet worked in the following manner:

  • The DateAdd() function called DateAdd 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 the DateAdd function with the $date and 7 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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *