Home > database >  Powershell get timezone with DST
Powershell get timezone with DST

Time:03-29

In powershell I'm using

(Get-Timezone).BaseUtcOffset

to get the UTC offset of a computer which gives me 1h for my timezone. That is technically correct since I'm in CET in winter (UTC 1) and CEST in summer (UTC 2). Right now tho it is DST, so CEST (UTC 2) for me so I'm wondering how I could get this information in powershell since the above command tells me that my timezone is UTC 1 and doesn't mention DST at all.

As a workaround I currently use

$date = Get-Date
($date - $date.ToUniversalTime()).TotalMinutes

to get the offset from UTC of my timezone with DST. It evaluates to 120 minutes which is exactly what i need.

Output of Get-Timezone:

Id                         : W. Europe Standard Time
DisplayName                : (UTC 01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien
StandardName               : Mitteleuropäische Zeit
DaylightName               : Mitteleuropäische Sommerzeit
BaseUtcOffset              : 01:00:00
SupportsDaylightSavingTime : True

Output of $date - $date.ToUniversalTime():

Days              : 0
Hours             : 2
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 72000000000
TotalDays         : 0,0833333333333333
TotalHours        : 2
TotalMinutes      : 120
TotalSeconds      : 7200
TotalMilliseconds : 7200000

CodePudding user response:

Yes, you can use the static method IsDaylightSavingTime on the TimeZoneInfo class to get this information from a desired DateTime:

$now = [DateTime]::Now
[System.TimeZoneInfo]::Local.IsDaylightSavingTime($now) # Returns $True or $False

CodePudding user response:

thank you Bender the Greatest you got me on the right path. The class System.TimeZoneInfo has a function that does what i want:

[System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes

gives me 120 minutes

  • Related