3

I have the following code that take in input scheduleConfiguration.Time in the UTC and return ticks in local time.

scheduleConfiguration.Time equal {9/13/2015 10:00:00 AM} in UTC

var localTime = scheduleConfiguration.Time.ToLocalTime(); {9/13/2015 1:00:00 PM} in Local
var executionTime = new TimeSpan(localTime.TimeOfDay.Ticks);

I changed my data contract scheduleConfiguration.Time, so I need to use TimeSpan TimeOfDay instead of DateTime Time, but I need to have the same executionTime. So I do next

var local time = DateTime.Now.Date.Add(scheduleConfiguration.TimeOfDay);//{9/13/2015 10:00:00 AM} in Local
var executionTime = new TimeSpan(localTime.Ticks);

So I have the difference in 3 hours (and I have UTC +3 time zone)

How to get the same result as for DateTime in the first situation?

Anatoly
  • 1,908
  • 4
  • 25
  • 47
  • good questions! I think you need to create localTime in the second variant to UTC. So `{9/13/2015 10:00:00 AM}` must be in UTC, not in the local time –  Sep 13 '15 at 14:36
  • 2
    You should check this answer: http://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset – Wouter Sep 13 '15 at 14:49
  • @Wouter How to use DateTimeOffset in my situation? – Anatoly Sep 13 '15 at 15:08

1 Answers1

5

You should use SpecifyKind method.

var time = DateTime.SpecifyKind(DateTime.Now.Date.Add(span), DateTimeKind.Utc);      
Console.WriteLine(time.ToLocalTime());