2

I'm struggling with a problem. i have a string date looks like this, "2015-05-02 01:00:00", extracted from a database.

I know that it's british time, but my local time is belgian time. I'm trying to store the date in UTC and in (CEST or CET depend of the season), converting it from the British time i've extract.

I tried to set Kind property to British time, but the result seems to be in local or utc time. So, i can do half of the job, but not the rest (e.g. I still need the CEST/CET time).

I tried to use this :

string dateString = (string) line["stringDate"];

DateTime ukTime = DateTime.Parse(dateString, new CultureInfo("en-GB", false));
DateTime belgianTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(ukTime, "Romance Standard Time");

The result is the same for both ukTime and belgianTime: 2015-05-02 01:00:00 with kind = unspecified.

It should be 2015-05-02 02:00:00 for belgianTime

7
  • Just waiting for Jon Skeet to show up to recommend using Joda :-) Commented Jul 26, 2017 at 8:16
  • Have you tried ukTime.ToLocalTime()? Commented Jul 26, 2017 at 8:16
  • I tried in a fiddle & given this result in belgianTime: 2015/5/2 3:00:00. If I used ToLocalTime it has same result as ukTime. Commented Jul 26, 2017 at 8:18
  • @derape wouldn't it be Noda for C#? Isn't Joda for Java? Commented Jul 26, 2017 at 8:19
  • @DannyGoodall ahh right! I got mixed up Commented Jul 26, 2017 at 8:21

3 Answers 3

1

If you just add the source time zone to the conversion method it gives the desired answer, even without specifying the IFormatProvider.

string dateString = (string) line["stringDate"];

DateTime ukTime = DateTime.Parse(dateString);
DateTime belgianTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(ukTime,
    "GMT Standard Time",
    "Romance Standard Time");

This gives time Kind == Unspecified. However if you use:

var belgianTime = TimeZoneInfo.ConvertTime(ukTime,
    TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"), 
    TimeZoneInfo.Local);

for the conversion, you get Kind == Local

Sign up to request clarification or add additional context in comments.

Comments

0

Use the following line of code.

var time = DateTime.Parse(DateTime.Now.ToString());
        var clientZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
        var utcTime = TimeZoneInfo.ConvertTimeToUtc(time, clientZone);

Comments

0

You have to use the ConvertTime method. ConvertTime

a quick sample -

string s = "2015-05-02 01:00:00";

var dt = new DateTime(2015, 05, 02, 1, 0, 0);

var t = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"), 
                     TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.