2012-06-14 101 views
10

我试图显示我的系统与时区的本地时间。我怎么能以这种格式显示时间的最简单的方法可能任何系统:有关将Powershell显示当前时区与时区

时间:上午08时零零分34秒EST

我目前使用下面的脚本:

$localtz = [System.TimeZoneInfo]::Local | select-object -expandproperty Id 
if ($localtz -match "Eastern") {$x = " EST"} 
if ($localtz -match "Pacific") {$x = " PST"} 
if ($localtz -match "Central") {$x = " CST"} 
"Time: " + (get-date).Hour + ":" + (get-date).Minute + ":" + (get-date).Second + $x 

我希望能够在不依赖简单逻辑的情况下显示时间,但能够在任何系统上给出本地时区。谢谢!

回答

9

虽然这是一个有点天真......也许,这是获得一个一种方式没有开关声明的缩写:

[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1') 

我的regu年轻的表情可能会让人有些不满。

上述我的时区输出为EST。我做了一些看,因为我想看看其他GMT偏移设置的值是什么,但.NET在DateTimeTimeZoneInfo之间似乎没有很好的链接,所以我不能通过编程方式通过它们全部检查。对于返回StandardName的某些字符串,这可能无法正常工作。

编辑:我做了一些更多的研究改变了我的计算机上的时区手动检查这个和TimeZoneInfoGMT+12看起来是这样的:

PS> [TimeZoneInfo]::Local 

Id       : UTC+12 
DisplayName    : (GMT+12:00) Coordinated Universal Time+12 
StandardName    : UTC+12 
DaylightName    : UTC+12 
BaseUtcOffset    : 12:00:00 
SupportsDaylightSavingTime : False 

将会产生这样的结果对我的代码:

PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1') 
U+12 

所以,我想你必须检测StandardName是否看起来像是一组词或者只是偏移指定,因为它没有标准名称。

在美国以外的问题较少的人似乎也遵循了三个词格式:

PS> [TimeZoneInfo]::Local 

Id       : Tokyo Standard Time 
DisplayName    : (GMT+09:00) Osaka, Sapporo, Tokyo 
StandardName    : Tokyo Standard Time 
DaylightName    : Tokyo Daylight Time 
BaseUtcOffset    : 09:00:00 
SupportsDaylightSavingTime : False 

PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1') 
TST 
+0

工程就像一个美丽! –

+0

@Ken我有点尴尬,但我很高兴它做到了这一招:)。 – Shibumi

+1

'SA太平洋标准时间'变成'SPST';这并不准确。您提供的信息非常有帮助。我决定只是为了获得分钟抵消: '[System.TimeZone] :: CurrentTimeZone.GetUtcOffset([datetime] :: Now).TotalMinutes' – VertigoRay

1

我不知道任何可以为你工作的物体。你可以换逻辑的功能:

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 

甚至乘坐时区ID的缩写:

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

记得拨打'IsDaylightSavingTime'检查是否看'标准名称'(我认为这与'Id'相同,但不清楚,如果总是如此)或DaylightName属性。您还需要处理所有其他时区的默认情况。 – Richard

+0

@Richard默认情况下有什么问题? –

+0

没有一个。 – Richard

6

你应该看看DateTime format strings。尽管我不确定他们能否返回时区短名称,但您可以轻松获得UTC的偏移量。

$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date) 

这将返回:

8:00:34 AM -04:00 
+0

我欣赏格式化的帮助。但是,我仍然需要找出将时区关联的方法。 –

3

要厌恶定义另一个日期时间格式!使用现有的,如rfc 1123。甚至有一个Powershell快捷键!

get-Date -format r 

星期四,2012 6月14日16点44分18秒GMT

http://technet.microsoft.com/en-us/library/hh849887.aspx

+5

需要注意的是,这不是很准确:目前报道的是格林威治标准时间(格林尼治标准时间前1小时)。 – Richard

+0

但这不正确,因为当前时区不是GMT。我以前使用格式,但它总是给出GMT而不是正确的时区。 –

+6

无论你的时区是什么时区,它都会得到GMT,它是硬编码的 - (Get-Culture).DateTimeFormat.RFC1123Pattern, –

0

就联合几个脚本终于能够在我的域控制器上运行该脚本。脚本为域下连接的所有机器提供时间和时区的输出。 我们在应用程序服务器上遇到了一个主要问题,并使用此脚本来检查时间和时区。

  #Below Scripts provides the Time and TimeZone for the Connected Machines in a Domain 
      #Appends the Output to a text file with the time stamp 
      #Checks if the host is reachable or not via ping command 

      Start-Transcript -path C:\output.txt -append 
      $ldapSearcher = new-object directoryservices.directorysearcher; 
      $ldapSearcher.filter = "(objectclass=computer)"; 
      $computers = $ldapSearcher.findall(); 

      foreach ($computer in $computers) 
      { 
       $compname = $computer.properties["name"] 
       $ping = gwmi win32_pingstatus -f "Address = '$compname'" 
       $compname 
       if($ping.statuscode -eq 0) 
       { 
        try 
        { 
         $ErrorActionPreference = "Stop" 
         write-host “Attempting to determine timezone information for $compname…” 
         $Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname 

         $remoteOSInfo = gwmi win32_OperatingSystem -computername $compname 
         [datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)  

            if($Timezone) 
         { 
          foreach($item in $Timezone) 
          { 
           $TZDescription = $Timezone.Description 
           $TZDaylightTime = $Timezone.DaylightName 
           $TZStandardTime = $Timezone.StandardName 
           $TZStandardTime = $Timezone.StandardTime 

          } 
          write-host “Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n” 

         } 
        else 
         { 
          write-host ("something went wrong") 
         } 

        } 
        catch 
        { 
         write-host (" you have insufficient rights to query the computer or the RPC server is not available") 
        } 
        finally 
        { 
         $ErrorActionPreference = "Continue" 
        } 

       } 
       else 
         { 
          write-host ("Host $compname Not reachable from ping `n") 
         } 

      } 

      Stop-Transcript 
0

这是一个更好的答案:

$A = Get-Date     #Returns local date/time 
$B = $A.ToUniversalTime()  #Convert it to UTC 

# Figure out your current offset from UTC 
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset 

#Add the Offset 
$C = $B + $Offset.BaseUtcOffset 
$C.ToString() 

输出: 2017年3月20日下午11时55分55秒