Tutorial :Convert From Local Time to UTC (Given the local TimeZone)



Question:

I've dates in different time zones.

How to convert them to UTC giving the respective time zone.

Something like this:

Dim Dated as DateTime = TempDate.ConvertToUniversalTime(TimeZone)  


Solution:1

The DateTime object in .net provides the DateTime.ToUniversalTime method. Since you are using the VB Date type the DateTime.FromOADate and DateTime.ToOAdate will provide conversion between the Date and DateTime types (see Interop Considerations under Programming Tips here).

UPDATE: You might also want to check out Converting Times Between Time Zones.


Solution:2

Here's how to do it

Dim TimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")  Dim Dated As DateTime = TimeZoneInfo.ConvertTimeToUtc(TempDate, TimeZone)  


For the TimeZones IDs check:

TimeZone.GetSystemTimeZones  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »