0

On an application I am saving all dates into a database as UTC.

However, the users insert the dates in Lisbon timezone ...

I checked and Lisbon timezone is WEST in summer and WET the rest of the year.

WET = Western European Time
WEST = Western European Summer Time

I then tried the following:

DateTime now = DateTime.Now;
DateTime nowUtc = DateTime.UtcNow;

DateTime.SpecifyKind(now, DateTimeKind.Local);

DateTime testNowUtc = now.ToUniversalTime();

But testNowUtc becomes the same as now and not as nowUtc

How can I solve this?

** UPDATE **

I tried the following:

DateTime now = DateTime.Now;
DateTime nowUtc = DateTime.UtcNow;

String zone = TimeZoneInfo.Local.IsDaylightSavingTime(now) ? TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName;

DateTime testNowUtc = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.FindSystemTimeZoneById(zone));

But when I run it I get the error:

The time zone ID 'GMT Daylight Time' was not found on the local computer.

Am I missing something?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • http://stackoverflow.com/questions/7908343/list-of-timezone-ids-for-use-with-findtimezonebyid-in-c http://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimetoutc(v=vs.110).aspx – Aron Jul 26 '14 at 11:48
  • I added an update with code based on your suggestions but I get an error saying: "The time zone ID 'GMT Daylight Time' was not found on the local computer." – Miguel Moura Jul 26 '14 at 12:26
  • The timezone name is "GMT Standard Time". Whether daylight savings are in effect only depends on the date. Abbreviations like "WET" are arbitrary and ambiguous, lots of "CST" around. – Hans Passant Jul 26 '14 at 12:40

1 Answers1

0
DateTime now = DateTime.Now;
DateTime nowUtc = DateTime.UtcNow;

DateTime testNowUtc = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.Local);

The point of TimeZoneInfo is that it does daylight saving automatically.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • Yes, I tried that but it does not give me the correct time. It gives me a one hour difference ... I think it is because of summer time ... – Miguel Moura Jul 26 '14 at 13:22
  • No. It should give you the correct answer. However, your original question was on West European time, whilst your computer is set to GMT. You aren't giving me the right information. – Aron Jul 26 '14 at 14:16