Share via


New-TimeSpan

Applies To: Windows PowerShell 2.0

Creates a TimeSpan object.

Syntax

New-TimeSpan [[-Start] <DateTime>] [[-End] <DateTime>] [<CommonParameters>]

New-TimeSpan [-Days <int>] [-Hours <int>] [-Minutes <int>] [-Seconds <int>] [<CommonParameters>]

Description

The New-TimeSpan cmdlet creates a TimeSpan object that represents a time. interval You can use a TimeSpan object to add or subtract time from DateTime objects.

Without parameters, a "New-Timespan" command returns a timespan object that represents a time interval of zero.

Parameters

-Days <int>

Indicates the days in the time span. The default is 0.

Required?

false

Position?

named

Default Value

0

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-End <DateTime>

Indicates the end of a time span. The default is the current date and time.

Required?

false

Position?

2

Default Value

Current date and time

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Hours <int>

Indicates the hours in the time span. The default is zero.

Required?

false

Position?

named

Default Value

0

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Minutes <int>

Indicates the minutes in the time span. The default is 0.

Required?

false

Position?

named

Default Value

0

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Seconds <int>

Indicates the length of the time span in seconds. The default is 0.

Required?

false

Position?

named

Default Value

0

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Start <DateTime>

Indicates the start of a time span. Enter a string that represents the date and time, such as "3/15/09" or a DateTime object, such as one from a Get-Date command.

The default is the current date and time.

Required?

false

Position?

1

Default Value

Current date and time

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

<CommonParameters>

This command supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, OutBuffer, OutVariable, WarningAction, and WarningVariable. For more information, see about_CommonParameters.

Inputs and Outputs

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.

Inputs

System.DateTime

You can pipe a DateTime object that represents that start time to New-TimeSpan.

Outputs

System.Timespan

New-TimeSpan returns an object that represents the time span.

Example 1

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

Description

-----------

This command creates a TimeSpan object with a duration of 1 hour and 25 minutes and stores it in a variable named $timespan. It displays a representation of the TimeSpan object.

Example 2

C:\PS>new-timespan -end (get-date -year 2010 -month 1 -day 1)

Description

-----------

This example creates a new TimeSpan object that represents the interval between the time that the command is run and January 1, 2010.

This command does not require the Start parameter, because the default value of the Start parameter is the current date and time.

Example 3

C:\PS>$90days = new-timespan -days 90

C:\PS> (get-date) + $90days

Description

-----------

These commands return the date that is 90 days after the current date.

See Also

Concepts

Set-Date
Get-Date