简体   繁体   中英

ASP.NET DateTime.Now wrong Time

I'm having a strange result when calling DateTime.Now So, i did the following .apsx and executed on the server.

<%@ Import Namespace="System.Globalization.CultureInfo" %>

<%
    System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() 
%>

<%= DateTime.UtcNow %>
<br>
<%= DateTime.Now %>
<br>
<%= TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now) %>
<br>
<%= DateTime.Now.IsDaylightSavingTime() %>
<br>
<%= TimeZone.CurrentTimeZone.StandardName %>
<br>
<%= TimeZone.CurrentTimeZone.DaylightName %>
<br>
<%= TimeZoneInfo.Local.Id %>

Here is the result:

10/01/2018 13:32:58 
10/01/2018 10:32:58 
-02:00:00 
False 
E. South America Standard Time 
E. South America Daylight Time 
E. South America Standard Time

Why DateTime.Now returned a -03:00:00 and not a -02:00:00 time? Why IsDaylightSavingTime returns false, but it should be True?

Try DateTimeOffset.Now instead of DateTime.Now . It will report the offset from UTC properly ( DateTimeOffset.Now.Offset ).

It is not DST-aware, use TimeZoneInfo for that.

You will keep running into gotchas with DateTime and anything concerning timezones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM