将时间(小时和分钟)插入日期时间对象

时间:2018-07-11 13:19:01

标签: powershell datetime

我有一个脚本,该脚本接受开始日期和开始时间作为单独的参数。如果未提供开始日期,则假定他们希望该日期为今天,如果没有开始时间,则假定其应为“现在是任何时间”。

我不想(太多)限制用户输入日期或时间的方式。例如,我希望他们能够输入do -StartDate (Get-Date),所以我需要能够接受该对象(包括时间),然后,因为没有输入StartTime,所以我需要替换任何时间当前时间的对象。

我想说的是,如果$StartDate变量中有一个时间,然后使用它,但是如果他们有-StartDate 07/10/2018,那么我仍然需要添加时间。

经过一些搜索,这是我尝试过的结果:

PS C:\> Get-Date

Tuesday, July 10, 2018 3:15:58 PM

PS C:\> $StartDate = (Get-Date).AddDays(1)
>> $StartTime = [DateTime]::ParseExact((Get-Date -Format HH:mm).ToString(),"HH:mm",[System.Globalization.CultureInfo]::InvariantCulture)
>> [datetime]$StartDate = $StartDate
>> $StartDate = $StartDate.Add([System.Timespan]::Parse($StartTime))
>> $StartDate

Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid TimeSpan."
At line:4 char:1
+ $StartDate = $StartDate.Add([System.Timespan]::Parse($StartTime))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Wednesday, July 11, 2018 3:16:10 PM

所以,我认为问题是,“我该如何花费特定的时间并将其插入日期时间对象?”或者也许有更好的方法可以实现我的目标,那也很酷。

谢谢。

3 个答案:

答案 0 :(得分:3)

您始终可以通过Get-Date的各个参数来构造日期,例如

Get-Date -Hour 8 -Minute 30

将为您提供今天的日期,时间为8:30。

您还可以像这样仅获取DateTime值的日期部分:

$today = (Get-Date).Date

添加几天(或只有几天)

$tomorrow = $today.AddDays(1)

,然后添加所需的时间跨度:

$time = New-Timespan -Hour 8 -Minute 30
$tomorrow.Add($time)

也可以采用菊花链式连接:

$time = New-Timespan -Hour 8 -Minute 30
(Get-Date).Date.AddDays(1).Add($time)

在您的特定情况下,您可以将开始日期作为DateTime值,剥离时间,然后添加时间跨度,例如通过字符串参数:

Param(
    [Parameter(Mandatory=$true)]
    [DateTime]$StartDate,
    [Parameter(Mandatory=$true)]
    [String]$StartTime
)

$StartDate.Date.Add([Timespan]::Parse($StartTime))

并像这样调用它:

foo -StartDate (Get-Date) -StartTime '8:30'

否则您将在小时和分钟中使用不同的参数:

Param(
    [Parameter(Mandatory=$true)]
    [DateTime]$StartDate,
    [Parameter(Mandatory=$true)]
    [ValidateRange(0,23)]
    [Integer]$Hour,
    [Parameter(Mandatory=$false)]
    [ValidateRange(0,59)]
    [Integer]$Minute = 0
)

$StartDate.Date.Add((New-Timespan -Hour $Hour -Minute $Minute))

并像这样调用脚本/函数:

foo -StartDate (Get-Date) -Hour 8 -Minute 30

答案 1 :(得分:0)

您可以使用$StartDate = (Get-Date).Date将DateTime对象的时间部分清零。 要仅获取DateTime对象的时间部分(作为TimeSpan对象返回),可以使用(Get-Date).TimeOfDay

要测试用户是否给您一个包括日期在内的完整日期(如果未加上当前时间),可以为您解决这个问题:

if ($StartDate.TimeOfDay -eq [TimeSpan]::Zero) {
    $StartDate = $StartDate.AddSeconds($StartTime.TotalSeconds)
}

$StartDate

答案 2 :(得分:0)

使用Ansgar Wiechers的建议,此修改后的功能可以按照我希望的方式工作:

function foo {
    Param(
        [Parameter(Mandatory = $true)]
        [DateTime]$StartDate,

        [String]$StartTime
    )

    If (-NOT($StartTime)) {
        $currentTime = Get-Date

        $StartDate.Date.Add((New-Timespan -Hour $currentTime.Hour -Minute $currentTime.Minute))
    }
    Else {
       $StartDate.Date.Add([Timespan]::Parse($StartTime))
    }
}

# Tomorrow, at the current time
foo -StartDate (Get-Date).AddDays(1)

# Tomorrow at 13:30
foo -StartDate (Get-Date).AddDays(1) -StartTime '13:30'

# July 5, 2018 at the current time
foo -StartDate "07-05-2018"
相关问题