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:

image

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

AddHoursAdd/subtract based on a number of hours.

AddMillisecondsAdd/subtract based on a number of milliseconds. ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.