使用 Powershell 在特定时区将字符串转换为 DateTime 对象 [英] Convert String to DateTime Object in specific timezone with Powershell

查看:125
本文介绍了使用 Powershell 在特定时区将字符串转换为 DateTime 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对 Powershell 的了解有限,我正在尝试将当前的字符串转换为:

With my limited knowledge of Powershell, I'm trying to convert a string in the current from:

2020-01-23 10:06:07

到时区东部标准时间中的日期时间对象.最终,我希望能够使用与 UTC 的正确偏移量格式化为 ISO8601 标准.

to a datetime object in the timezone Eastern Standard Time. Ultimately I want to be able to format to ISO8601 standard with the correct offset from UTC.

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

这可以在 powershell 中实现吗?我已经查看了 ConvertTimeFromUtc,但是我很难最初指定时区是东部标准时间而不是 GMT.

Is this achievable within powershell? I have looked at ConvertTimeFromUtc however I am struggling to initially specify that the timezone is Eastern Standard Time instead of GMT.

推荐答案

处理给定的名义日期(未指定相对于>与哪个时区相关)作为 EST(美国东部标准时间)时区之一:

To treat a given nominal date (one that is unspecified with respect to what time zone it relates to) as one in the EST (US Eastern Standard Time) time zone:

即把'2020-01-24 03:00:57'这样的日期字符串转换成一个 [datetimeoffset] 实例,将这个未指定的时区字符串表示为 EST(东部标准时间)时区的本地日期/时间(可能带有 DST(夏令时)节省时间)偏移量),然后可以将其格式化为 ISO 8601 格式,其中包括结果日期的特定 UTC 偏移量.

That is, convert a date string such as '2020-01-24 03:00:57' into a [datetimeoffset] instance that represents this unspecified-in-terms-of-time-zone string as a date/time local to the EST (Eastern Standard Time) time zone (possibly with DST (daylight-saving time) offset applied), which can then be formatted in a ISO 8601 format that includes the resulting date's specific UTC offset.

# 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',根据需要.

The above yields '2020-01-24T03:00:57-05:00', as desired.

使用 DST 窗口输入日期,例如 '2020-07-24 03:00:57',它会产生
'2020-07-24T03:00:57-04:00' - 请注意 UTC 偏移量现在如何减少一小时.

With a DST-window input date such as '2020-07-24 03:00:57', it would yield
'2020-07-24T03:00:57-04:00' - note how the UTC offset is now one hour less.

另见:System.DateTime([datetime],作为 PowerShell 类型文字),System.DateTimeOffset ([datetimeoffset]) 和 System.TimeZoneInfo ([TimeZoneInfo]) 类型,以及 标准日期和时间格式字符串.

See also: The System.DateTime ([datetime], as a PowerShell type literal), System.DateTimeOffset ([datetimeoffset]), and System.TimeZoneInfo ([TimeZoneInfo]) types, and Standard date and time format strings.

以下是一个具有不同前提的相关用例:

The following is a related use case with a different premise:

要将给定的本地日期转换为其等效的 EST:

To translate a given local date into its EST equivalent:

即,将本地时间点(例如通过 Get-Date 获得的时间点)转换为 EST 时区中的等效时间.

That is, translate a local point in time, such as obtained by Get-Date, into the equivalent time in the EST time zone.

# 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'.

The above yields a string such as '2020-01-23T16:44:41-05:00'.

这篇关于使用 Powershell 在特定时区将字符串转换为 DateTime 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆