[英]How to calculate difference in minutes between START and END times in a file using Powershell or cmd
The file has the following content.该文件具有以下内容。
START Sun 04/17/2022 13:01:44.13
END Sun 04/17/2022 14:41:50.60
I'm trying to find a way to automate how many minutes it took from END
to START
time, I don't care about the seconds, just need to know how many minutes took to run.我试图找到一种方法来自动化从
END
到START
时间花费了多少分钟,我不关心秒数,只需要知道运行需要多少分钟。 In this example it took 100 minutes but I had to calculate manually.在这个例子中,它花了 100 分钟,但我不得不手动计算。 I'd appreciate any feed back.
如果有任何反馈,我将不胜感激。
Assuming that your input file is named file.txt
:假设您的输入文件名为
file.txt
:
$dates = [datetime[]] ((Get-Content file.txt) -replace '^\w+ +')
[int] ($dates[1] - $dates[0]).TotalMinutes # -> 100
Your date-time strings are in a format that can be cast to [datetime]
directly.您的日期时间字符串采用可以直接转换为
[datetime]
的格式。
To extract the date-time string from each line, the first whitespace-separated token must be removed, which the regex-based -replace
operator can do:要从每一行中提取日期时间字符串,必须删除第一个以空格分隔的标记,基于正则表达式的
-replace
运算符可以执行此操作:
-replace '^\w+ +'
matches one or more ( +
) word characters ( \w
, letters, digits or _
) at the start ( ^
) of the string, followed by one more spaces, matching something like END
. -replace '^\w+ +'
在字符串的开头 ( ^
) 匹配一个或多个 ( +
) 单词字符( \w
、字母、数字或_
),后跟一个空格,匹配类似END
的内容。 Since no replacement string is specified, the matched string is effectively removed . -replace
can operate on an array as its LHS, in which case the operation is performed on each element; -replace
可以将数组作为其 LHS 进行操作,在这种情况下,将对每个元素执行操作; therefore, all lines of the input file can serve as the input at once, as returned by Get-Content
.因此,输入文件的所有行都可以立即作为输入,由
Get-Content
返回。
Subtracting two [datetime]
instances yields a [timespan]
instance representing the span of time between the two points of time:减去两个
[datetime]
实例会产生一个[timespan]
实例,表示两个时间点之间的时间跨度:
.TotalMinutes
property reports the span in fractional minutes (as a [double]
instance).TotalMinutes
属性以小数分钟报告跨度(作为[double]
实例)[int]
yields an integral number of minutes, using half-to-even midpoint-rounding.[int]
会产生整数分钟数。New-TimeSpan
and the ParseExact()
or TryParse()
method of from an object instance of DateTime
will help you here. New-TimeSpan
和DateTime
对象实例的ParseExact()
或TryParse()
方法将在此处为您提供帮助。
$Start = [datetime]::ParseExact('04/17/2022 13:01:44.13', 'MM/dd/yyyy HH:mm:ss.ff', $null)
$End = [datetime]::ParseExact('04/17/2022 14:41:50.60', 'MM/dd/yyyy HH:mm:ss.ff', $null)
New-TimeSpan -Start $Start -End $End
This will provide an output like the below:这将提供如下所示的输出:
Days : 0
Hours : 1
Minutes : 40
Seconds : 6
Milliseconds : 470
Ticks : 60064700000
TotalDays : 0.0695193287037037
TotalHours : 1.66846388888889
TotalMinutes : 100.107833333333
TotalSeconds : 6006.47
TotalMilliseconds : 6006470
Access specifically minutes by doing something similar to the below:通过执行类似于以下的操作来访问特定的会议记录:
> $result = New-TimeSpan -Start $Start -End $End
> $result.TotalMinutes
100.107833333333
# Round up to 2 decimal places
> [Math]::Round($result.TotalMinutes, 2)
100.11
# Round up to 0 decimal places
> [Math]::Round($result.TotalMinutes, 0)
100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.