In the year 2013, the United Kingdom won't start daylight savings time until March 31st.
Still, TimeZoneInfo.ConvertTimeToUtc
subtracts one hour when I try to convert a given time to UTC time.
Why is this? Do I need to tell this method that daylight savings time is not active?
var now = new DateTime(DateTime.Now.Ticks, DateTimeKind.Unspecified);
var convertedTime = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time"));
Console.WriteLine(convertedTime); // subtracts one hour, even though England time and GMT should be equal
You can use DateTime. TryParse() : which will convert the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.
ToUniversalTime() Method in C# This method is used to convert the value of the current DateTime object to Coordinated Universal Time (UTC). Syntax: public DateTime ToUniversalTime ();
The error mentioned above normally arises when the date literal is not proper and cannot be converted from the string into DateTime or date. This error results due to a number of reasons, which we will discuss in detail along with the solution set.
When we try to convert date or time from character string following error arises sometimes. “Conversion failed when converting date and/or time from character string.” The error mentioned above normally arises when the date literal is not proper and cannot be converted from the string into DateTime or date.
Time settings typically don't affect internet connectivity, however if it is significantly off, PKI certificates can show as expired or not yet valid, causing browser warnings. Another case is if your endpoint is required to authenticate with the network before being granted access. (RADIUS, 802.1x).
For example, the TimeZoneInfo.ConvertTimemethod can be used to convert a DateTimefrom one time zone to another. Passing it a whole date makes no sense, as only a single point in time on that date could possibly be converted.
You are simply using the wrong time zone. England is in the "GMT Standard Time" zone.
The "W. Europe Standard Time" zone is for continental Europe, Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna, UTC+01:00.
[NOTE: This answer assumes the computer running the code is in the GMT timezone]
It's because "W. Europe Standard time" isn't the same timezone as UTC according to .Net (even though it should be). This is very odd, and seems like a bug in the .Net libraries.
Due to this, you've inadvertently told it to convert a time to UTC and passed it a UTC (i.e. GMT time) but then told it that the time is in "W. Europe Standard time", which is an hour ahead of UTC.
So naturally, the resulting time in an hour different.
Step by step:
var now = new DateTime(DateTime.Now.Ticks, DateTimeKind.Unspecified);
This sets now
to the time in UTC.
var convertedTime = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time")
This converts now
to UTC, assuming that now
is in "W. Europe Standard time", i.e. UTC+1. But it isn't! Therefore the result is incorrect.
[EDIT in response to comments below]
Note that "W. Europe Standard time"
should be UTC, but it isn't for some reason. And when you do this:
TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time")
it returns UTC+1 rather than the (apparently) correct UTC. It even changes the name to "W. Europe Daylight Time"
. Seems like it might be a curious bug?
[Second Edit]
Check out the Microsoft Time Zone Index Values (and also here). (Note Neither of those links are actually for Windows 7, but they contain the same definitions.)
It has an entry for "W. Europe Standard Time" which definitely states (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
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