Create datetime object of other timezone in Powershell

后端 未结 2 700
南方客
南方客 2021-01-19 04:24

Basically I am trying to find a way to convert datetime of a particular timezone to other timezone while taking DST into consideration too. e.g.

What is

2条回答
  •  攒了一身酷
    2021-01-19 05:16

    This solution worked well for me:

    $cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
    $csttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $cstzone)
    

    You can then manipulate the $csttime variable just like a datetime object:

    Get-Date $csttime.AddHours(-1) -f "MM\dd\yyyy HH:mm:ss"
    

    References: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v=vs.110).aspx http://technet.microsoft.com/en-us/library/ee692801.aspx

提交回复
热议问题