簡體   English   中英

使用 Powershell 將字符串轉換為特定時區的 DateTime 對象

[英]Convert String to DateTime Object in specific timezone with Powershell

由於我對 Powershell 的了解有限,我正在嘗試將當前的字符串從:

2020-01-23 10:06:07

到時區Eastern Standard Time中的 datetime 對象。 最終,我希望能夠使用與 UTC 的正確偏移量格式化為 ISO8601 標准。

20-01-23T10:06:07-05:00

這在 powershell 中可以實現嗎? 我查看了ConvertTimeFromUtc但是我很難最初指定時區是東部標准時間而不是 GMT。

DataTime 本身沒有關於時區的信息。 如果您想將 UTC 日期轉換為其他時區日期,您可以使用您提到的 ConvertTimeFromUtc。 例子:

$DateTime = Get-Date "2020-01-23 10:06:07"
$TimeZone = [TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")
[TimeZoneInfo]::ConvertTimeFromUtc($DateTime, $TimeZone)

或者您可以從任何時區轉換。 獲取時區:

[TimeZoneInfo]::GetSystemTimeZones() | select Id | sort Id

從一個時區轉換為另一個時區:

$DateTime = Get-Date "2020-01-23 10:06:07"
$SourceTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")
$DestinationTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById("Azores Standard Time")
[TimeZoneInfo]::ConvertTime($DateTime, $SourceTimeZone, $DestinationTimeZone)

將給定的標稱日期(未指定其相關時區的日期)視為 EST(美國東部標准時間)時區中的日期:

也就是說,將日期字符串(例如'2020-01-24 03:00:57'轉換為[datetimeoffset]實例,該實例將此未指定時區字符串表示為本地日期/時間EST(東部標准時間)時區(可能應用了 DST(夏令時)偏移量),然后可以將其格式化為 ISO 8601 格式,其中包括結果日期的特定 UTC 偏移量。

# Construct a nominal [datetime] instance whose .Kind property value is
# Unspecified (which means unspecified with respect to any particular
# time zone), which a cast from a string achieves:
$nominalDate = [datetime] '2020-01-24 03:00:57'

# Determine the target time zone.
# Note: On macOS and Linux, use 'America/New_York' (ICU library IDs).
$tz = [TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time')

# Get the UTC offset for the nominal date (.Kind == Unspecified), 
# which is interpreted as local to that time zone.
# The offset is returned as a [timespan] instance that properly reflects
# DST, if the date falls into the DST window of the target time zone.
# If the input date is ambiguous or invalid, standard time is assumed.
$utcOffset = $tz.GetUtcOffset($nominalDate)

# Construct a [datetimeoffset] instance with the UTC offset determined above.
# This in effect creates a date that represents the nominal date in the 
# target time zone, using that time zone's DST-appropriate UTC offset.
$dto = [DateTimeOffset]::new($nominalDate.Ticks, $utcOffset)

# Format according to ISO 8601 with UTC offset, but remove the
# fractional-seconds part:
# Note: With the standar "o" format specifier, only [datetimeoffset]
#       instances include their UTC offset in the resulting string,
#       not [datetime] instances.
$dto.ToString('o') -replace '\.\d+(?=-)'

根據需要,以上結果會產生'2020-01-24T03:00:57-05:00'

使用 DST 窗口輸入日期,例如'2020-07-24 03:00:57' ,它將產生
'2020-07-24T03:00:57-04:00' - 請注意 UTC 偏移現在如何減少一小時。

另請參閱: System.DateTime[datetime] ,作為 PowerShell 類型文字)、 System.DateTimeOffset[datetimeoffset] )和System.TimeZoneInfo[TimeZoneInfo] )類型,以及標准日期和時間格式字符串


以下是具有不同前提的相關用例:

將給定的本地日期轉換為等效的 EST:

即,將本地時間點(例如通過Get-Date點)轉換為對應的 EST 時區時間。

# Start with a local date, in any time zone.
# (A [datetime] instance whose .Kind property value is Local, though
#  Unspecified would work the same).
# Alternatively, start with a UTC date (where .Kind is UTC)
$localDate = Get-Date

# Translate it to Eastern Standard time, as a [datetimeoffset] instance.
# Note: Casting $localDate to [datetimeoffset] is crucial to ensure
#       that a [datetimeoffset] with the proper UTC offset is returned.
#       Without it, you'd get a [datetime] instance that is nominally
#       the correct time, but has an Unspecified .Kind value.
#       Also, only a [datetimeoffset] instance includes a UTC offset
#       when stringified with format string 'o'
$dtoEST = [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
  [datetimeoffset] $localDate, 
  'Eastern Standard Time'
)

# Format according to ISO 8601 with UTC offset, but remove the
# fractional-seconds part:
$dtoEST.ToString('o') -replace '\.\d+(?=-)'

以上產生一個字符串,例如'2020-01-23T16:44:41-05:00'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM