DateTime in RFC-1123 gives inaccurate time zone

If I get the RFC-1123 formatting date of a DateTime object, it gives the current local time, but gives the time zone as GMT (which is inaccurate).
DateTime.Now.ToString("r");
is returning
Fri, 12 Feb 2010 16:23:03 GMT

At 4:23 in the afternoon, but my time zone is UTC + 10 (plus, we are currently observing daylight saving time).

Now I can get the return value that is “correct” by first converting to UTC:
DateTime.UtcNow.ToString("r");
returns
Fri, 12 Feb 2010 05:23:03 GMT

However, ideally, I would like to get the correct time zone, which, I think, will be Fri, 12 Feb 2010 16:23:03 +1100

Passing to the current CultureInfo does not change anything. I could get the UTC offset from TimeZoneInfo.Local.GetUtcOffset (...) and format the timezone line, but removing the GMT bit and replacing it seems futile.

Is there a way to get it to enable the correct time zone?

+3
source share
2 answers

The .NET implementation always expresses the result as if it were GMT, regardless of the time offset of the actual date.

Using DateTime.Now.ToString("r");, you essentially say String.Format("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", DateTime.Now);which is a .NET RFC1123 format string, as specified in MSDN - RFC1123 ("r", "r") Format specifier .

To get the required behavior, you probably should use String.Formatand replace the fixed "GMT" section of the specifier with the time offset specifier:

+4

DateTime.UtcNow.ToString ("R"), GMT, .

+4

Source: https://habr.com/ru/post/1732471/


All Articles