简体   繁体   English

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

[英]Format time difference between 2 date time vars in a Powershell script

I'm using Powershell to run an AWS backup to S3 buckets and write out a continuing log file.我正在使用 Powershell 运行 AWS 备份到 S3 存储桶并写出一个连续的日志文件。 I need to show how long the backup takes in the log file.我需要在日志文件中显示备份需要多长时间。 I've tried a number of methods and can't seem to find one that works.我尝试了多种方法,但似乎找不到一种有效的方法。 If I remove the -Format... command it shows the time difference but it's ugly.如果我删除 -Format... 命令,它会显示时差,但很难看。 This code is one of my many attempts这段代码是我的许多尝试之一

$StartTime = get-date
>>> RUN THE BACKUP
$EndTime1 = Get-Date
$DURATION = $EndTime1 - $StartTime  -Format 'hh:mm:ss'

Either use ToString() directly on the resulting TimeSpan , or use the -f string format operator:直接在生成的TimeSpan上使用ToString() ,或使用-f字符串格式运算符:

$duration.ToString('hh\:mm\:ss')
# or 
'{0:hh\:mm\:ss}' -f $duration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM