C# TimeZoneInfo ConvertTimeToUtc(DateTime, TimeZoneInfo)
Description
TimeZoneInfo ConvertTimeToUtc(DateTime, TimeZoneInfo)
converts
the time in a specified time zone to Coordinated Universal Time (UTC).
Syntax
TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo)
has the following syntax.
public static DateTime ConvertTimeToUtc(
DateTime dateTime,
TimeZoneInfo sourceTimeZone
)
Parameters
TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo)
has the following parameters.
dateTime
- The date and time to convert.sourceTimeZone
- The time zone of dateTime.
Returns
TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo)
method returns The Coordinated Universal Time (UTC) that corresponds to the dateTime parameter.
The DateTime object's Kind property is always set to DateTimeKind.Utc.
Example
The following example retrieves the current date from the local system and converts it to Coordinated Universal Time (UTC), then converts it to Tokyo Standard Time, and finally converts from Tokyo Standard Time back to UTC.
using System;/*from ww w . j a v a 2 s. c o m*/
public class Example
{
public static void Main()
{
DateTime thisTime = DateTime.Now;
Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
Console.WriteLine(" UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
// Get Tokyo Standard Time zone
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);
Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
tst.DaylightName : tst.StandardName, tstTime);
Console.WriteLine(" UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));
}
}
The code above generates the following result.