19

Should I use DateTime.Now or DateTime.UtcNow in HttpCookie.Expires and HttpCachePolicy.SetExpires?

Cookies are sending 'GMT' time, but I don't know what happen if I send DateTime.Now.AddDays(3) if I would be in GMT+5. Same with Expires HTTP header (sec 14.21).

What should I use?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
vtortola
  • 34,709
  • 29
  • 161
  • 263

2 Answers2

18

It doesn't matter in this case.

Internally, the first thing .SetExpires does is convert your supplied datetime into UTC, before setting it on the cookie.

Bear in mind, as long as your datetime consumer uses the DateTime class correctly, then the two are the same - it is just that one is "baselined" to UTC and the other isn't:

20110701T14:00:00-1:00 (British Summer Time)

and

20110701T13:00:00+0:00 (UTC)

represent exactly the same datetime, namely 1pm UTC.

As long as the consumer handles this correctly (which it seems to, having looked in reflector) then it makes no difference.

If you were taking this and passing it in as a time string, then of course, it may well make a difference, but not in this case.

You can see the effect with the following code (assuming you are not in UTC yourself - if you are - change your settings to test!). They both output the same datetime, once you've asked for it to be converted to UTC.

WriteDateTime(DateTime.Now);
WriteDateTime(DateTime.UtcNow);

public static void WriteDateTime(DateTime dateTime)
{
   Console.WriteLine(dateTime.ToUniversalTime().ToLongTimeString());   
}
Rob Levine
  • 40,328
  • 13
  • 85
  • 111
  • 2
    It's best to use DateTime.UtcNow for this because it's faster than DateTime.Now. – Alexander van Trijffel Aug 24 '12 at 19:09
  • This answer is incorrect. DateTime does not store any offset and hence DateTime.Now and DateTime.UtcNow are very different. The question relates to what do the browsers expect and the correct answer is that they expect a value with 0 offset (UTC). So the correct answer is actually the one offered by @Phil Carson below. – Talon Oct 31 '16 at 08:40
  • 1
    @Talon: DateTime has a Kind property, which is set to DateTimeKin.Utc for DateTime.UtcNow and to DateTimeKind.Local for DateTime.Now; Although not used in comparison (just tick equality), it is affectiting conversions. ToUniversalTime() is a Noop if Kind is Utc, does a Local timezone shift if .Unspecified or .Local; Thus the HttpCookie method is able to tell them apart when "converting" them to Universal on usage as Rob indicated. – user6649841 Jun 25 '21 at 12:50
1

You should be using DateTime.UtcNow method because thats the time standard used for cookies. UTC is equivilant to GMT.

From MSDN: System.DateTime.UtcNow

Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

Refer to this for an explanation between them.

Community
  • 1
  • 1
Phil Carson
  • 884
  • 8
  • 18
  • I know. the problem is that even in the MSDN page, they use DateTime.Now http://msdn.microsoft.com/en-us/library/system.web.httpcookie.expires.aspx and that's bugging me. I don't know if they just don't care o if HttpCookie.Expires already transform to GMT :P – vtortola Jan 31 '11 at 10:54
  • -1 - I don't believe this is correct - it shouldn't make any difference which you use. – Rob Levine Jan 31 '11 at 11:18
  • Rob, as you have pointed out the technically using either due to the implementation of the method will work. There are other considerations: – Phil Carson Feb 01 '11 at 10:33
  • such as that the output of the SetExpires method is a UTC DateTime so by passing in a UTC DateTime you show clarity of implementation. Another consideration although very unlikly in this case but important as a general principle if the implementation of the SetExpire method (a method outside our codebase) were to change so that it did not convert (or converted incorrectly) then a bug would be introduced into our code and then we would then a fix. (sorry for the split comment, I hit enter and then got interrupted while I was editing the comment ... ;) – Phil Carson Feb 01 '11 at 10:46
  • @RobLevine It makes a big difference which one you use. DateTime does not store any offset so DateTime.Now could be "2016-10-10 05:00" yet DateTime.UtcNow could be "2016-10-09-23:00". DateTimeOffset.Now and DateTimeOffset.UtcNow would be the same thing. – Talon Oct 31 '16 at 08:43