.net - Get Timezone Offset of Server in C# -
how can timezone offset of physical server running code? not date object or other object in memory.
for example, following code output -4:00:00
:
<%= timezone.currenttimezone.getutcoffset(new datetime()) %>
when should -03:00:00
because of daylight savings
new datetime()
give january 1st 0001, rather current date/time. suspect want current utc offset... , that's why you're not seeing daylight saving offset in current code.
i'd use timezoneinfo.local
instead of timezone.currenttimezone
- may not affect things, better approach. timezoneinfo
should pretty replace timezone
in code. can use getutcoffset
:
var offset = timezoneinfo.local.getutcoffset(datetime.utcnow);
(using datetime.now
should work well, involves magic behind scenes when there daylight saving transitions around now. datetime
actually has four kinds rather advertised three, it's simpler avoid issue entirely using utcnow
.)
or of course use noda time library instead of bcl rubbish ;) (if you're doing lot of date/time work i'd thoroughly recommend - - if you're only doing 1 bit, overkill.)
Comments
Post a Comment