Create a TimeSpan object. The resulting object can be used to add or subtract time from a DateTime object to create additional DateTime objects.

      New-TimeSpan [-start] DateTime [-end] DateTime

      New-TimeSpan [-days int] [-hours int] [-minutes int]
            [-seconds int] [CommonParameters]

   The timespan can either be a specific number of days/hours/minutes
   or you can enter a Start/End and PowerShell will calculate the difference.

       The start of the timespan.

   -End DateTime
       The end of the timespan, may be piped. Default=Now

   -Days int
       Days in the timespan.

   -Hours int
       Hours in the timespan.

   -Minutes int
       Minutes in the timespan.

   -Seconds int
       Seconds in the timespan.


Create a TimeSpan object of duration 1 hour plus 90 minutes and store it in a variable named $mytspan:

PS C:\> $mytspan = new-timespan -hour 1 -minute 90

Create a new TimeSpan object for Jan 1970 - Jan 1971:

PS C:\> $mytspan = new-timespan (get-date -year 1970 -month 01) (get-date -year 1971 -month 01)

“I wasted time, and now doth time waste me” ~ Shakespeare (Richard II)

Related PowerShell Cmdlets

Get-Date - Get current date and time.
- Set system time on the host system.
Equivalent bash command: date - Display or change the date.

Copyright © 1999-2024
Some rights reserved