header image

Date Additions

Posted by: | August 29, 2008 | No Comment |

In http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1658.entry we saw how to get the difference between two dates. Let’s look at working out a day in the future.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Reference > Working with Dates and Times  Date Arithmetic

Listing 2.17

$now = Get-Date
"60 days from now: $($now.AddDays(60).DateTime)"
"60 days ago:      $($now.AddDays(-60).DateTime)"
 

Set a variable to todays date using get-date.  We could use get-date directly but the times will creep out as we experiment – use a variable for consistency.  We then use AddDays to add the number of days we want to project forward and backwards – notice the use of negative number of days for getting dates in the past!!  The datetime property forces the display in the format I want – see http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1657.entry

As well as days there are methods to add other time periods:

Add
AddDays
AddHours
AddMilliseconds
AddMinutes
AddMonths
AddSeconds
AddTicks
AddYears

Add – adds a time span object. A tick is 1/10000th of a second. The rest are self explanatory and used in the same way as AddDays.

 

Share this post :

 

Technorati Tags: ,

under: PowerShell original