在Powershell脚本中格式化两个日期时间变量之间的时差

问题描述 投票:0回答:1

我正在使用Powershell将AWS备份运行到S3存储桶并写出连续的日志文件。我需要在日志文件中显示备份需要多长时间。我尝试了多种方法,但似乎找不到有效的方法。如果我删除了-Format ...命令,它会显示时差,但是很难看。此代码是我的许多尝试之一

$StartTime = get-date
>>> RUN THE BACKUP
$EndTime1 = Get-Date
$DURATION = $EndTime1 - $StartTime  -Format 'hh:mm:ss'
powershell datetime-format
1个回答
1
投票

直接在结果ToString()上使用TimeSpan,或使用-f字符串格式运算符:

$duration.ToString('hh\:mm\:ss')
# or 
'{0:hh\:mm\:ss}' -f $duration
© www.soinside.com 2019 - 2024. All rights reserved.