Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 549
vtortola Avatar asked Jan 31 '11 10:01

vtortola


People also ask

Should I use DateTime now or DateTime UtcNow?

UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime. Now gives the date and time as it would appear to someone in your current locale.

What is difference between DateTime now and DateTime UtcNow in C#?

The property Now of the DateTime class returns the current date and time of the machine running the code, expressed in the computer's local time. The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format.

What is 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). public: static property DateTime UtcNow { DateTime get(); };

What timezone is DateTime now?

DateTime. UtcNow is the time according to the Coordinated Universal Time standard and is the same across timezones. This means that DateTime. UtcNow has the same value in the GMT +2 timezone and in the GMT -7 timezone and all other timezones.


2 Answers

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());   
}
like image 130
Rob Levine Avatar answered Oct 21 '22 10:10

Rob Levine


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.

like image 37
Phil Carson Avatar answered Oct 21 '22 12:10

Phil Carson