如何使用PowerShell或CMD计算文件开始和结束时间之间的分钟数差异
该文件具有以下内容。
START Sun 04/17/2022 13:01:44.13
END Sun 04/17/2022 14:41:50.60
我正在尝试找到一种方法来自动化从结束
到开始
时间的时间,我不在乎秒,只需要知道多少几分钟跑步。 在此示例中,花了100分钟,但我必须手动计算。 我感谢任何回馈。
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.
In this example it took 100 minutes but I had to calculate manually.
I'd appreciate any feed back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的输入文件被命名为
file.txt
:您的日期时间字符串的格式可以直接施加到
[dateTime]
直接。要从每行中提取日期时间字符串,必须删除第一个whitespace分开的令牌,基于REGEX的
-replace
操作员可以做:
p> p> p> p> p> p> p>
),然后是另一个空间,匹配-replace'^\ w++'
匹配一个或多个(+
)字字符(\ w
,字母,数字或<在字符串的开始(^
)处的代码> _end> end
之类的东西。由于未指定替换字符串,因此有效地删除了匹配的字符串。-replace
可以在数组上以LHS为单位,在这种情况下,操作是在每个元素上执行的;因此,输入文件的所有行都可以立即用作输入,如get-content
。减去两个
[dateTime]
实例产生[timespan]
表示两个时间点之间的时间跨度:.totalminutes
属性在分数分钟内报告跨度(作为[double]
实例)[int]
,使用一半到甚至中点转。Assuming that your input file is named
file.txt
:Your date-time strings are in a format that can be cast to
[datetime]
directly.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 '^\w+ +'
matches one or more (+
) word characters (\w
, letters, digits or_
) at the start (^
) of the string, followed by one more spaces, matching something likeEND
. 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; therefore, all lines of the input file can serve as the input at once, as returned byGet-Content
.Subtracting two
[datetime]
instances yields a[timespan]
instance representing the span of time between the two points of time:.TotalMinutes
property reports the span in fractional minutes (as a[double]
instance)[int]
yields an integral number of minutes, using half-to-even midpoint-rounding.new-timespan
和parseexact()
或tryparse()
从dateTime
的对象实例的方法将有所帮助你在这里。这将提供类似以下输出的输出:
通过执行与以下类似的操作,专门访问分钟:
New-TimeSpan
and theParseExact()
orTryParse()
method of from an object instance ofDateTime
will help you here.This will provide an output like the below:
Access specifically minutes by doing something similar to the below: