如何在 Windows 上使用 PowerShell 以 Windows FILETIME格式獲取當前時間?
類似于這個關于除 Windows 之外的 Linux 的答案,使用 WindowsFILETIME格式(64 位值表示自 UTC 時間 1601 年 1 月 1 日以來的 100 納秒間隔數),并且最好像上述答案一樣簡單。
uj5u.com熱心網友回復:
# Returns a FILETIME timestamp representing the current UTC timestamp,
# i.e. a [long] value that is the number of 100-nanosecond intervals
# since midnight 1 Jan 1601, UTC.
[datetime]::UtcNow.ToFileTime()
替代方案:[dateime]::Now.ToFileTimeUtc()或[datetimeoffset]::Now.ToFileTime()
要將這樣的FILETIME值轉換回[datetime]實體:
[datetime]::FromFileTime(
[datetime]::UtcNow.ToFileTime()
)
注意:上面產生了一個本地 [datetime]實體(它的.Kind屬性是Local)。附加.ToUniversalTime()以獲取 UTC 實體(其中.Kind是Utc)。
或者,用于[datetimeoffset]::FromFileTime()獲取明確的時間戳,可以按原樣使用或根據需要轉換為本地 ( .LocalDateTime) 或 UTC ( .UtcDateTime)[datetime]實體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/402101.html
