Display current time with time zone in PowerShell

前端 未结 7 826
盖世英雄少女心
盖世英雄少女心 2021-02-14 02:06

I\'m trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:

Time: 8:00:34 AM ES

7条回答
  •  野性不改
    2021-02-14 02:44

    I'm not aware of any object that can do the work for you. You could wrap the logic in a function:

    function Get-MyDate{
    
        $tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
            Eastern    {'EST'; break}
            Pacific    {'PST'; break}
            Central    {'CST'; break}
        }
    
        "Time: {0:T} $tz" -f (Get-Date)
    }
    
    Get-MyDate
    

    Or even take the initials of the time zone id:

    $tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
    "Time: {0:T} $tz" -f (Get-Date)
    

提交回复
热议问题