如何使用PowerShell或CMD计算文件开始和结束时间之间的分钟数差异

发布于 2025-01-23 11:58:50 字数 261 浏览 0 评论 0原文

该文件具有以下内容。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2025-01-30 11:58:50

假设您的输入文件被命名为file.txt

$dates = [datetime[]] ((Get-Content file.txt) -replace '^\w+ +')
[int] ($dates[1] - $dates[0]).TotalMinutes  # -> 100
  • 您的日期时间字符串的格式可以直接施加到[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:

$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.

    • 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 like 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; therefore, all lines of the input file can serve as the input at once, as returned by Get-Content.

  • Subtracting two [datetime] instances yields a [timespan] instance representing the span of time between the two points of time:

    • Its .TotalMinutes property reports the span in fractional minutes (as a [double] instance)
    • Casting that value to [int] yields an integral number of minutes, using half-to-even midpoint-rounding.
醉城メ夜风 2025-01-30 11:58:50

new-timespanparseexact()tryparse()dateTime的对象实例的方法将有所帮助你在这里。

$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

这将提供类似以下输出的输出:

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

通过执行与以下类似的操作,专门访问分钟:

> $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

New-TimeSpan and the ParseExact() or TryParse() method of from an object instance of DateTime will help you here.

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文