다음을 통해 공유


New-TimeSpan

Creates a TimeSpan object.

구문

Date (기본값)

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

Time

New-TimeSpan
    [-Days <Int32>]
    [-Hours <Int32>]
    [-Minutes <Int32>]
    [-Seconds <Int32>]
    [<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.

예제

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.

$TimeSpan = New-TimeSpan -Hours 1 -Minutes 25
$TimeSpan
Days              : 0
Hours             : 1
Minutes           : 25
Seconds           : 0
Milliseconds      : 0
Ticks             : 51000000000
TotalDays         : 0.0590277777777778
TotalHours        : 1.41666666666667
TotalMinutes      : 85
TotalSeconds      : 5100
TotalMilliseconds : 5100000

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.

New-TimeSpan -End (Get-Date -Year 2010 -Month 1 -Day 1)

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

$90days = New-TimeSpan -Days 90
(Get-Date) + $90days

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

Example 4: Discover the TimeSpan since a file was updated

This command tells you how long it has been since the about_Remote help file was last updated. You can use this command format on any file, or 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, PowerShell uses the value of the LastWriteTime property as the value of the Start parameter.

Get-ChildItem $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

매개 변수

-Days

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

매개 변수 속성

형식:Int32
Default value:None
와일드카드 지원:False
DontShow:False

매개 변수 집합

Time
Position:Named
필수:False
파이프라인의 값:False
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

-End

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

매개 변수 속성

형식:DateTime
Default value:Current date and time
와일드카드 지원:False
DontShow:False

매개 변수 집합

Date
Position:1
필수:False
파이프라인의 값:False
속성 이름별 파이프라인의 값:True
나머지 인수의 값:False

-Hours

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

매개 변수 속성

형식:Int32
Default value:None
와일드카드 지원:False
DontShow:False

매개 변수 집합

Time
Position:Named
필수:False
파이프라인의 값:False
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

-Minutes

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

매개 변수 속성

형식:Int32
Default value:None
와일드카드 지원:False
DontShow:False

매개 변수 집합

Time
Position:Named
필수:False
파이프라인의 값:False
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

-Seconds

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

매개 변수 속성

형식:Int32
Default value:None
와일드카드 지원:False
DontShow:False

매개 변수 집합

Time
Position:Named
필수:False
파이프라인의 값:False
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

-Start

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 ([IO.FileInfo]), to the Start parameter of New-TimeSpan.

매개 변수 속성

형식:DateTime
Default value:Current date and time
와일드카드 지원:False
DontShow:False
별칭:LastWriteTime

매개 변수 집합

Date
Position:0
필수:False
파이프라인의 값:True
속성 이름별 파이프라인의 값:True
나머지 인수의 값:False

CommonParameters

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

입력

DateTime

You can pipe a DateTime object representing the start time to this cmdlet.

출력

TimeSpan

This cmdlet returns an object representing the time span.