Dealing with Dates and Times

Being able to do date/time calculations is very useful. Fortunately PowerShell provides all kinds of quick date/time calculations. Some of the more common tricks are shown in the following example:

PS>[DateTime]::NowTuesday, August 05, 2012 2:01:22 PMPS>([DateTime]::Now).AddDays(-1)Monday, August 04, 2012 2:01:44 PMPS>

Here, a .NET method is used to get a new value from the original object. This is done in a “single step,” in contrast to saving the object to a variable and then using the method on the variable. The use of a minus sign indicates that a value is being requested from the past.

Other common date/time methods include the following:

AddHours—Add/subtract based on a number ...

Get Microsoft® SQL Server 2012 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.