【问题标题】:Get just hour and minutes in Windows Powershell在 Windows Powershell 中仅获取小时和分钟
【发布时间】:2018-10-24 12:55:04
【问题描述】:

我是 Powershell 的新手,我只需要在脚本中获取小时和分钟。

我目前有这个:

【问题讨论】:

  • 如果你使用Get-Help Get-Date -Full,你可以找到相当多的信息。也许它甚至会回答你的问题。

标签: powershell scripting powershell-3.0


【解决方案1】:

你几乎拥有它;而不是g,使用:

  • hh 12 小时制的小时
  • HH 表示 24 小时格式的小时
  • mm 分钟

例如,Get-Date -Format hh:mm 会将 12 小时制返回为“04:55”,而Get-Date -Format HH 将仅返回 24 小时制的小时数。

Microsoft 记录了可能格式的完整列表:Custom Date and Time Format Strings

【讨论】:

    【解决方案2】:

    试试这个Get-Date -Format HH:mm

    【讨论】:

      【解决方案3】:

      如果您使用的是 Powershell (Core) 版本 7 或更高版本,则可以使用新的 TimeOnly 数据类型。您可以在 blog post 中找到更多关于这次的示例。

      $Time = [System.TimeOnly]::FromDateTime((Get-Date))

      $Time.ToString() 打印 10:02

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 2019-12-22
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多