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?
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.
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.
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(); };
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.
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());
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With