PowerShell: Find if time variable is in UTC or local time

How do we know if a variable of type [DateTime] is in UTC or Local time zone? Let us see how to achieve this using PowerShell.

First you need to ensure that date time information is stored in proper DateTime variable. To check you can invoke GetType() method on the variable that is containing the date time information and it tells us whether it is of date time type or not.

$Date = Get-Date

$Date . GetType()

Once it is confirmed then you can check if the time in the variable is a local time or in UTC timezone by reading the Kind property value. This property has 3 possible values.

  1. Local : The time represented is local time.
  2. UTC : The time represented is UTC.
  3. Unspecified : The time represented is not specified as either local time or Coordinated Universal Time (UTC).

Let us see a quick example:

$Date = Get-Date

$Date . Kind

$UTCDate = $Date . ToUniversalTime()

$UTCDate . kind

A sample screenshot of output is below:

Hope you liked it. Do you know how to find the exact time zone of a date time variable? I will come up with another post for it.

稿源:Techibee.com (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » PowerShell: Find if time variable is in UTC or local time

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录