在Powershell中格式化来自不同时区的时间/日期 [英] Formatting Time/Date from different time zones in Powershell
问题描述
我需要使用 Powershell 将 UTC 时间/日期输出转换为 ISO 8601(或类似)格式的东部标准时间,以便在 DOS 批处理文件中使用.
I need to convert UTC time/date output to Eastern Standard Time in ISO 8601 (or similar) format using Powershell for use in a DOS Batch file.
我对 Powershell 的经验很少,但以前修改过各种 DOS 批处理文件的现有 Powershell 命令.Powershell 可以轻松地从多个时区输出时间/日期,但不清楚它是否可以轻松地以数字格式显示该输出.
I have very little experience with Powershell, but have previously modified existing Powershell commands for various DOS batch files. Powershell can easily output time/date from numerous time zones, but it's not clear if it can readily display that output in a numerical format.
在DOS批处理文件中,可以调用Powershell以这种格式显示UTC时间--
In a DOS batch file, Powershell can be called to display UTC time in this format --
echo | powershell (get-date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss')
我需要调用其他时区,但输出是拼写为FullDateTimePattern" --
I need to call upon other time zones, but the output is 'FullDateTimePattern' which is spelled out --
echo | Powershell [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'Line Islands Standard Time')
我希望之前的命令可以(以某种方式)合并在一起,或者如果可以将Get-Date -format"说明符之一应用于第二个代码示例,或者是否可以将偏移值应用于UTC 时间/日期输出,或者是否有其他一些简单的代码解决方案(或实用程序)可以合并到 DOS 批处理文件中.
I was hoping that the previous commands could (somehow) be merged together, or if one of the 'Get-Date -format' specifiers could be applied to the second code sample, or if an offset value could be applied to the UTC time/date output, or if there is some other easy code solution (or utility) that I could incorporate into a DOS batch file.
推荐答案
将当前 UTC [datetime]
即时转换为指定格式的东部标准时间:
To convert the current UTC [datetime]
instant to Eastern Standard Time with the specified format:
powershell.exe -c "[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([datetime]::UtcNow, 'Eastern Standard Time').ToString('yyyy-MM-ddTHH:mm:ss')"
请注意以下额外改进:
不需要
echo |
:当使用带有(可能隐含)-Command
/-c
的命令调用 PowerShell 时,完成该命令后它会自动退出.(顺便说一句,echo |
不只是发送一个 换行符,它发送字符串ECHO is on.
,然后是一个换行符;要仅发送换行符,您必须使用echo.|
No need for
echo |
: When PowerShell is invoked with a command with (possibly implied)-Command
/-c
, it automatically exits after completing that command. (As an aside,echo |
doesn't send just a newline, it sends the stringECHO is on.
, followed by a newline; to send just a newline, you'd have to useecho. |
Option -c
(-Command
) 被显式使用,不仅是为了使意图更清晰,而且还便于以后向 PowerShell 的潜在过渡Core,其中 -Command
不再是默认值(-File
是).
Option -c
(-Command
) is used explicitly, not just to make the intent clearer, but also to ease potential later transition to PowerShell Core, where -Command
is no longer the default (-File
is).
虽然您的特定命令可以在没有双引号 ("..."
) 的情况下工作,但通常最好使用它们,以便包含 cmd.exe 的命令
元字符 - 例如,&
- 不要破坏.
While your specific command would work without the enclosing double quotes ("..."
), it's generally better to use them, so that commands that include cmd.exe
metacharacter - e.g., &
- don't break.
这篇关于在Powershell中格式化来自不同时区的时间/日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!