New-TimeSpan
创建 TimeSpan 对象。
语法
Date (默认值)
New-TimeSpan
[[-Start] <DateTime>]
[[-End] <DateTime>]
[<CommonParameters>]
Time
New-TimeSpan
[-Days <Int32>]
[-Hours <Int32>]
[-Minutes <Int32>]
[-Seconds <Int32>]
[-Milliseconds <Int32>]
[<CommonParameters>]
说明
New-TimeSpan
cmdlet 创建了一个表示时间间隔的 TimeSpan 对象。
可以使用 TimeSpan 对象从 DateTime 对象中添加或减去时间。
如果没有参数,New-TimeSpan
命令将返回表示零时间间隔的 TimeSpan 对象。
示例
示例 1:创建指定持续时间的 TimeSpan 对象
此命令创建 TimeSpan 对象,持续时间为 1 小时 25 分钟,并将其存储在名为 $TimeSpan
的变量中。 它将显示 TimeSpan 对象的表示形式。
$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
示例 2:为时间间隔创建 TimeSpan 对象
此示例创建一个新的 TimeSpan 对象,该对象表示运行命令的时间与 2010 年 1 月 1 日之间的间隔。
此命令不需要 Start 参数,因为 Start 参数的默认值为当前日期和时间。
New-TimeSpan -End (Get-Date -Year 2010 -Month 1 -Day 1)
示例 3:获取从当前日期起 90 天的日期
$90days = New-TimeSpan -Days 90
(Get-Date) + $90days
这些命令返回当前日期后的 90 天日期。
示例 4:发现自更新文件以后的 TimeSpan
此命令指示自上次更新 about_Remote 帮助文件以来的时长。 可以在任何文件或具有 LastWriteTime 属性的任何其他对象上使用此命令格式。
此命令的工作原理是, 的 New-TimeSpan
参数具有 LastWriteTime的别名。 通过管道将具有 LastWriteTime 属性的对象传递给 New-TimeSpan
时,PowerShell 将 LastWriteTime 属性的值用作 Start 参数的值。
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
指定时间跨度中的天数。 默认值为 0。
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
Time
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-End
指定时间跨度的结束时间。 默认值为当前日期和时间。
参数属性
类型: | DateTime |
默认值: | Current date and time |
支持通配符: | False |
不显示: | False |
参数集
Date
Position: | 1 |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | True |
来自剩余参数的值: | False |
-Hours
指定时间跨度中的小时数。 默认值为 0。
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
Time
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Milliseconds
指定时间跨度长度(以毫秒为单位)。 默认值为 0。
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
Time
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Minutes
指定时间跨度中的分钟数。 默认值为 0。
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
Time
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Seconds
指定时间跨度长度(以秒为单位)。 默认值为 0。
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
Time
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | False |
-Start
指定时间跨度的开始。 输入表示日期和时间的字符串,例如“3/15/09”或 DateTime 对象,例如 Get-Date
命令中的一个。 默认值为当前日期和时间。
可以使用 Start 或其别名 LastWriteTime。
LastWriteTime 别名允许通过管道将具有 LastWriteTime 属性的对象(如文件系统中的文件(如文件系统中的文件[IO.FileInfo]
)传递给 的 New-TimeSpan
参数。
参数属性
类型: | DateTime |
默认值: | Current date and time |
支持通配符: | False |
不显示: | False |
别名: | 最后写入时间 |
参数集
Date
Position: | 0 |
必需: | False |
来自管道的值: | True |
来自管道的值(按属性名称): | True |
来自剩余参数的值: | False |
CommonParameters
此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters。
输入
DateTime
您可以通过管道将 DateTime 对象(表示开始时间)传递给此 cmdlet。
输出
TimeSpan
此 cmdlet 返回表示时间跨度的对象。