I want to initialize DateTime to EST and then convert to Universal TIme

I want to initialize DateTime to Eastern Standard Time and then convert to universal time.

I have One DateTime (CurrentDT) whose TimeZone is not set, but it is in EST. he parsedExact from next sting

"ddd, d MMM yyyy H: mm: ss"

I wrote the following code to solve the problem.

  TimeZoneInfo currentTimeZone=null;
  currentTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");      
  CurrentDT=TimeZoneInfo.ConvertTime(CurrentDT, currentTimeZone);
  CurrentDT = CurrentDT.ToUniversalTime();

The problem is that it looks like I'm doing wrong. I am converting a DateTime that is in the local time zone to Eastern Standerd Time, and then to universal standard time. I do not know how I can initialize TimeZone from CurrentDt first. Please help me understand, thanks.

+4
2

, DateTime TimeZone, , DateTime TimeZone, UTC , - :

var UtcDateTime = TimeZoneInfo.ConvertTimeToUtc(CurrentDt, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); // 1st param is the date to convert and the second is its TimeZone
+1

() DateTimeOffset, :

var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var offset = est.GetUtcOffset();
var dataTimeOffset = new DateTimeOffset(dateTimeWithoutOffset, offset);
var result = dateTimeOffset.UtcDateTime;

, DateTimeOffset .

+1

Source: https://habr.com/ru/post/1616167/


All Articles