-3

There's plenty of info showing how to convert a DateTime object expressed in UTC to a specific time zone for output.

But if for example the DateTime object was known to be expressed as Australian Eastern Standard Time, how would I convert it to a new DateTime object where it is expressed as UTC?

I see that there is a .ToUniversalTime method, but this assumes the source is expressed in local time and doesn't allow you to specify the source time zone.

Quantum_Kernel
  • 303
  • 1
  • 7
  • 19
  • Does this help? https://learn.microsoft.com/en-us/dotnet/api/system.datetime.touniversaltime?view=netcore-3.1 – Christoph Lütjen Nov 01 '20 at 23:52
  • You can create dateTime with your local time and specify a specific timezone, and call `ToUniversalTime` – Pac0 Nov 01 '20 at 23:53
  • These methods assume the datetime is in local time. As far as I can see, your cant tell the method what the zone actually is unless it's UTC or Local. – Quantum_Kernel Nov 01 '20 at 23:58
  • 5
    DateTime is only useful and well defined for UTC and your local machine TZ. Use DateTimeOffset for everything else. – Jeremy Lakeman Nov 02 '20 at 00:06
  • Does this answer your question? [Creating a DateTime in a specific Time Zone in c#](https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp) – Mick Nov 02 '20 at 00:10
  • Also worth to evaluate: https://nodatime.org/ – Christoph Lütjen Nov 02 '20 at 00:11

1 Answers1

0

Provided what Jeremy Lakeman answer, i elaborated a converter from your Australian eastern standard to UTC time. Perhaps you will not need to use datetime ParseExact if it is your local time, but here is a working example:

var timeZone = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
var localTime = DateTime.ParseExact($"02/11/20 10:00:00", "MM/dd/yy hh:mm:ss", CultureInfo.GetCultureInfo("en-AU"));

Console.WriteLine($"your local time is: {localTime} and the UTC is {TimeZoneInfo.ConvertTimeToUtc(localTime, timeZone)}");

//output: your local time is: 2/11/2020 10:00:00 AM and the UTC is 2/11/2020 12:00:00 AM
nalnpir
  • 1,167
  • 6
  • 14