Export (0) Print
Expand All
Expand Minimize

Converting VBScript's DateAdd Function


Windows PowerShell


Definition: Returns a date to which a specified time interval has been added.

DateAdd

No scripting language (or at least none that we know of) can predict the future. However, many of them at least enable you to predict when the future will occur. For example, suppose today is 9/21/2006. What will the date be 37 days from now? In Windows PowerShell you can determine that by using the Get-Date Cmdlet along with the appropriate method. For example, this command calculates the date 37 days from the current date (using the AddDays() method) and stores that value in the variable $a



$a = (get-date).AddDays(37)

If this command is run on 9/21/2006 you should get the following when you echo back the value of $a:



Saturday, October 28, 2006 11:33:27 AM

Of course, you aren’t limited to working only with the AddDays() method. Here are other date arithmetic methods available to you:



(get-date).AddHours(37)
(get-date).AddMilliseconds(37)
(get-date).AddMinutes(37)
(get-date).AddMonths(37)
(get-date).AddSeconds(37)
(get-date).AddTicks(37)
(get-date).AddYears(37)

You can also combine these methods in a single command. This command calculates the time 2 hours and 34 minutes from now. To do that, it first uses AddHours() to determine the time 2 hours from now, then takes the resulting value and uses the AddMinutes() method to determine the time 34 minutes from then. (Note the use of parentheses, which ensures that AddHours() is run first and AddMinutes() is run only when AddHours() is done.) Here’s the command:



$a = ((get-date).AddHours(2)).AddMinutes(34)


Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft