本文整理汇总了C#中System.TimeZoneInfo.ConvertTimeToUtc方法的典型用法代码示例。如果您正苦于以下问题:C# TimeZoneInfo.ConvertTimeToUtc方法的具体用法?C# TimeZoneInfo.ConvertTimeToUtc怎么用?C# TimeZoneInfo.ConvertTimeToUtc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了TimeZoneInfo.ConvertTimeToUtc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Get time in local time zone
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 example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
// Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
// UTC Time: 12/6/2013 6:57:51 PM
// Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
// UTC Time: 12/6/2013 6:57:51 PM
示例2: DateTime
DateTime datNowLocal = DateTime.Now;
Console.WriteLine("Converting {0}, Kind {1}:", datNowLocal, datNowLocal.Kind);
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowLocal), TimeZoneInfo.ConvertTimeToUtc(datNowLocal).Kind);
Console.WriteLine();
DateTime datNowUtc = DateTime.UtcNow;
Console.WriteLine("Converting {0}, Kind {1}", datNowUtc, datNowUtc.Kind);
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowUtc), TimeZoneInfo.ConvertTimeToUtc(datNowUtc).Kind);
Console.WriteLine();
DateTime datNow = new DateTime(2007, 10, 26, 13, 32, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNow, datNow.Kind);
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNow), TimeZoneInfo.ConvertTimeToUtc(datNow).Kind);
Console.WriteLine();
DateTime datAmbiguous = new DateTime(2007, 11, 4, 1, 30, 00);
Console.WriteLine("Converting {0}, Kind {1}, Ambiguous {2}", datAmbiguous, datAmbiguous.Kind, TimeZoneInfo.Local.IsAmbiguousTime(datAmbiguous));
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datAmbiguous), TimeZoneInfo.ConvertTimeToUtc(datAmbiguous).Kind);
Console.WriteLine();
DateTime datInvalid = new DateTime(2007, 3, 11, 02, 30, 00);
Console.WriteLine("Converting {0}, Kind {1}, Invalid {2}", datInvalid, datInvalid.Kind, TimeZoneInfo.Local.IsInvalidTime(datInvalid));
try
{
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datInvalid), TimeZoneInfo.ConvertTimeToUtc(datInvalid).Kind);
}
catch (ArgumentException e)
{
Console.WriteLine(" {0}: Cannot convert {1} to UTC.", e.GetType().Name, datInvalid);
}
Console.WriteLine();
DateTime datNearMax = new DateTime(9999, 12, 31, 22, 00, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNearMax, datNearMax.Kind);
Console.WriteLine(" ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNearMax), TimeZoneInfo.ConvertTimeToUtc(datNearMax).Kind);
Console.WriteLine();
//
// This example produces the following output if the local time zone
// is Pacific Standard Time:
//
// Converting 8/31/2007 2:26:28 PM, Kind Local:
// ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
//
// Converting 8/31/2007 9:26:28 PM, Kind Utc
// ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
//
// Converting 10/26/2007 1:32:00 PM, Kind Unspecified
// ConvertTimeToUtc: 10/26/2007 8:32:00 PM, Kind Utc
//
// Converting 11/4/2007 1:30:00 AM, Kind Unspecified, Ambiguous True
// ConvertTimeToUtc: 11/4/2007 9:30:00 AM, Kind Utc
//
// Converting 3/11/2007 2:30:00 AM, Kind Unspecified, Invalid True
// ArgumentException: Cannot convert 3/11/2007 2:30:00 AM to UTC.
//
// Converting 12/31/9999 10:00:00 PM, Kind Unspecified
// ConvertTimeToUtc: 12/31/9999 11:59:59 PM, Kind Utc
//