Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer may surprise you but it is <em>NO</em>. You cannot add days, weeks, months, or years without the time zone influencing the result.</p> <p>The reason is that not all local days have 24 hours. Depending on the time zone, the rules for that zone, and whether DST is transitioning in the period in question, some "days" may have 23, 23.5, 24, 24.5 or 25 hours. (If you are trying to be precise, then instead use the term "standard days" to indicate you mean exactly 24 hours.)</p> <p>As an example, first set your computer to one of the USA time zones that changes for DST, such as Pacific Time or Eastern Time. Then run these examples:</p> <p>This one covers the 2013 "spring-forward" transition:</p> <pre><code>DateTime local1 = new DateTime(2013, 3, 10, 0, 0, 0, DateTimeKind.Local); DateTime local2 = local1.AddDays(1); DateTime utc1 = local1.ToUniversalTime(); DateTime utc2 = utc1.AddDays(1); DateTime local3 = utc2.ToLocalTime(); Debug.WriteLine(local2); // 3/11/2013 12:00:00 AM Debug.WriteLine(local3); // 3/11/2013 1:00:00 AM </code></pre> <p>And this one covers the 2013 "fall-back" transition:</p> <pre><code>DateTime local1 = new DateTime(2013, 11, 3, 0, 0, 0, DateTimeKind.Local); DateTime local2 = local1.AddDays(1); DateTime utc1 = local1.ToUniversalTime(); DateTime utc2 = utc1.AddDays(1); DateTime local3 = utc2.ToLocalTime(); Debug.WriteLine(local2); // 11/4/2013 12:00:00 AM Debug.WriteLine(local3); // 11/3/2013 11:00:00 PM </code></pre> <p>As you can see in both examples - the result was an hour off, one direction or the other.</p> <p>A couple of other points:</p> <ul> <li>There is no <code>AddWeeks</code> method. Multiply by 7 and add days instead.</li> <li>There is no <code>ToUtcTime</code> method. I think you were looking for <code>ToUniversalTime</code>.</li> <li>Don't call <code>DateTime.Now.ToUniversalTime()</code>. That is redundant since inside <code>.Now</code> it has to take the UTC time and convert to local time anyway. Instead, use <code>DateTime.UtcNow</code>.</li> <li>If this code is running on a server, you shouldn't be calling <code>.Now</code> or <code>.ToLocalTime</code> or ever working with <code>DateTime</code> that has a <code>Local</code> kind. If you do, then you are introducing the time zone of the <em>server</em> - not of the user. If your users are not in the same time zone, or if you ever deploy your application somewhere else, you will have problems.</li> <li>If you want to avoid these kind of problems, then look into <a href="http://nodatime.org" rel="noreferrer">NodaTime</a>. It's API will prevent you from making common mistakes.</li> </ul> <p>Here's what you should be doing instead:</p> <pre><code>// on the client DateTime local = new DateTime(2013, 3, 10, 0, 0, 0, DateTimeKind.Local); DateTime utc = local.ToUniversalTime(); string zoneId = TimeZoneInfo.Local.Id; // send both utc time and zone to the server // ... // on the server TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId); DateTime theirTime = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi); DateTime newDate = theirTime.AddDays(1); Debug.WriteLine(newDate); // 3/11/2013 12:00:00 AM </code></pre> <p>And just for good measure, here is how it would look if you used Noda Time instead:</p> <pre><code>// on the client LocalDateTime local = new LocalDateTime(2013, 3, 10, 0, 0, 0); DateTimeZone zone = DateTimeZoneProviders.Tzdb.GetSystemDefault(); ZonedDateTime zdt = local.InZoneStrictly(zone); // send zdt to server // ... // on the server LocalDateTime newDate = zdt.LocalDateTime.PlusDays(1); Debug.WriteLine(newDate); // 3/11/2013 12:00:00 AM </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload