-2

I got the Date and time(utc time) from database and i need to convert to cst timezone. I tried following code but its not convert date and time.

DateTimeOffset returnTime = TimeZoneInfo.ConvertTime(time, timeZone);

Input

time = {3/11/2018 3:24:19 AM}

timeZone = {(UTC-06:00) Central Time (US & Canada)}

Day light saving is Enabled

Expected Output

returnTime = {3/10/2018 10:24:19 PM}

Actual Output

returnTime = {3/11/2018 03:24:19 AM}

Please help to solve this problem.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Note that `DateTime` instance doesn't have daylight saving component for certain timezones, you need to adjust it manually. Try adding `TimeZoneInfo.Local` if the DB value already in UTC format. – Tetsuya Yamamoto Apr 03 '18 at 09:42
  • Are you sure you need to do this? If you are able, it's preferable to just return the UTC DateTime to the user application and allow it to be displayed in the user's local timezone. – pmcilreavy Apr 03 '18 at 10:14
  • What `.Kind` is the input DateTime? Definitely `UTC`? – pmcilreavy Apr 03 '18 at 10:29
  • @TetsuyaYamamoto time is DateTime variable i already passed Offset value to that. – M.Nishanthan Apr 03 '18 at 10:47
  • @pmcilreavy in here i need to get the date form returnTime value for date comparison thats why. – M.Nishanthan Apr 03 '18 at 10:48
  • Your code here is not a *complete* example. Please expand it to be complete. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Matt Johnson-Pint Apr 03 '18 at 17:02

1 Answers1

2

Use TimeZoneInfo.ConvertTimeFromUtc and I think your problem will go away. Also ensure the .Kind of the input DateTime is 'UTC'.

// a date before DST starts
var beforeDstUtc = new DateTime(2018, 3, 1, 13, 0, 0, DateTimeKind.Utc);
// a date after DST starts
var afterDstUtc = new DateTime(2018, 4, 1, 13, 0, 0, DateTimeKind.Utc);

var cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

var cstTime1 = TimeZoneInfo.ConvertTimeFromUtc(beforeDstUtc, cstZone);
var cstTime2 = TimeZoneInfo.ConvertTimeFromUtc(afterDstUtc, cstZone);

var expectedBeforeDstLocal = new DateTime(2018, 3, 1, 07, 0, 0, DateTimeKind.Local);
var expectedAfterDstLocal = new DateTime(2018, 4, 1, 08, 0, 0, DateTimeKind.Local);

// should be -6 hours
Assert.AreEqual(expectedBeforeDstLocal, cstTime1);
// should be -5 hours
Assert.AreEqual(expectedAfterDstLocal, cstTime2);
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37
  • For `TimeZoneInfo.ConvertTimeFromUtc` in here its not working during daylight saving Enabled time. i already tried. – M.Nishanthan Apr 03 '18 at 10:41
  • 1
    @M.Nishanthan really? I updated my answer to show that it does take DST into account. – pmcilreavy Apr 03 '18 at 10:54
  • @M.Nishanthan it shows that if we start with a UTC DateTime before daylight savings begins (e.g. 1st March) and convert to CST then we get -6 hours but if we start with a UTC DateTime which is after daylight savings (e.g. 1st April) then we get a local CST DateTime which is -5 hours. So it seems to me ConvertTimeFromUtc does handle DST. Also please see this answer - https://stackoverflow.com/questions/23158015/does-converttimefromutc-and-touniversaltime-handle-dst – pmcilreavy Apr 04 '18 at 12:33