PowerShell Logo Small

New-TimeSpan



This is the built-in help made by Microsoft for the command 'New-TimeSpan', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Creates a TimeSpan object.

SYNTAX


New-TimeSpan [[-Start] [<DateTime>]] [[-End] [<DateTime>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
[<System.String>]] [<CommonParameters>]
New-TimeSpan [-Days [<Int32>]] [-Hours [<Int32>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
[<System.String>]] [-Minutes [<Int32>]] [-Seconds [<Int32>]] [<CommonParameters>]



Search powershellhelp.space

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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293994
Get-Date
Set-Date

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

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



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

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



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

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



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










-------------------------- EXAMPLE 4 --------------------------

PS C:\>dir $pshome\en-us\about_remote.help.txt | new-timespan

Days : 321
Hours : 21
Minutes : 59
Seconds : 22
Milliseconds : 312
Ticks : 278135623127728
TotalDays : 321.916230471907
TotalHours : 7725.98953132578
TotalMinutes : 463559.371879547
TotalSeconds : 27813562.3127728
TotalMilliseconds : 27813562312.7728

# Equivalent to:

PS C:\>new-timespan -start (dir $pshome\en-us\about_remote.help.txt).lastwritetime



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.