Powershell 添加当前时间以计算得到的时间值

huangapple go评论63阅读模式
英文:

Powershell Add a value with current time to get calculated time

问题

我想要将天、小时或分钟添加到当前时间以获取我的计算时间。到目前为止,我正在使用以下代码,它正在工作,但是否有改进的方法?

$ts = New-TimeSpan -Days 0 -Hours 8 -Minutes 40
$ot = (Get-Date).Add($ts)
$ft = (Get-Date $ot).ToString('hh:mm')

上面的代码可以给您一个计算的时间,但我们可以将其简化成一行吗?

英文:

I want to add days or hours or minutes to the current time get my calculated time . As of now I'm using below code , it is working but is there any way we could improve

$ts = New-TimeSpan -Days 0 -Hours 8 -Minutes 40

$ot = (get-date) + $ts 

$ft = get-date $ot -Format hh:mm

Above code can give you a calculated time but can we make it into a single line ?

答案1

得分: 0

你可以将所有这些组合成一行代码:

$ft = (Get-Date) + (New-TimeSpan -Days 0 -Hours 8 -Minutes 40) | Get-Date -Format hh:mm
英文:

You can combine all of that into a single line as

$ft = (Get-Date) + (New-TimeSpan -Days 0 -Hours 8 -Minutes 40) | Get-Date -Format hh:mm

huangapple
  • 本文由 发表于 2023年5月29日 14:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定