New-TimeSpan

Updated: August 9, 2015

New-TimeSpan

Creates a TimeSpan object.

Syntax

Parameter Set: Date
New-TimeSpan [[-Start] <DateTime> ] [[-End] <DateTime> ] [ <CommonParameters>]

Parameter Set: Time
New-TimeSpan [-Days <Int32> ] [-Hours <Int32> ] [-Minutes <Int32> ] [-Seconds <Int32> ] [ <CommonParameters>]




Detailed 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<Int32>

Specifies the days in the time span. The default value is 0.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-End<DateTime>

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


Aliases

none

Required?

false

Position?

2

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Hours<Int32>

Specifies the hours in the time span. The default value is zero.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Minutes<Int32>

Specifies the minutes in the time span. The default value is 0.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Seconds<Int32>

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


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Start<DateTime>

Specifies 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 value is the current date and time.

You can use Start or its alias, LastWriteTime. The LastWriteTime alias lets you pipe objects that have a LastWriteTime property, such as files in the file system (System.Io.FileIO), to the Start parameter of New-TimeSpan.


Aliases

LastWriteTime

Required?

false

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.

Inputs

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

  • System.DateTime

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


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.TimeSpan

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


Examples

Example 1: Create a TimeSpan object for a specified duration

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.


PS C:\> $TimeSpan = New-TimeSpan -Hour 1 -Minute 25

Example 2: Create a TimeSpan object for a time interval

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.


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

Example 3: Get the date 90 days from the current date

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


PS C:\> $90days = New-TimeSpan -Days 90
PS C:\>(Get-Date) + $90days

Example 4: Discover the TimeSpan since a file was updated

This command tells you how long it has been since the about_remote.help.txt file was last updated. You can use this command format on any file, and on any other object that has a LastWriteTime property.

This command works because the Start parameter of New-TimeSpan has an alias of LastWriteTime. When you pipe an object that has a LastWriteTime property to New-TimeSpan, Windows PowerShell uses the value of the LastWriteTime property as the value of the Start parameter.


PS C:\> dir $pshome\en-us\about_remote.help.txt | New-TimeSpan
PS C:\> # Equivalent to:

PS C:\>New-TimeSpan -Start (dir $pshome\en-us\about_remote.help.txt).lastwritetime

Related topics

Community Additions

ADD
Show: