Bagikan melalui


TimeZoneInfo.ConvertTimeToUtc Metode

Definisi

Mengonversi tanggal dan waktu menjadi Waktu Universal Terkoordinasi (UTC).

Overload

ConvertTimeToUtc(DateTime)

Mengonversi tanggal dan waktu yang ditentukan ke Waktu Universal Terkoordinasi (UTC).

ConvertTimeToUtc(DateTime, TimeZoneInfo)

Mengonversi waktu dalam zona waktu tertentu menjadi Waktu Universal Terkoordinasi (UTC).

ConvertTimeToUtc(DateTime)

Sumber:
TimeZoneInfo.cs
Sumber:
TimeZoneInfo.cs
Sumber:
TimeZoneInfo.cs

Mengonversi tanggal dan waktu yang ditentukan ke Waktu Universal Terkoordinasi (UTC).

public:
 static DateTime ConvertTimeToUtc(DateTime dateTime);
public static DateTime ConvertTimeToUtc (DateTime dateTime);
static member ConvertTimeToUtc : DateTime -> DateTime
Public Shared Function ConvertTimeToUtc (dateTime As DateTime) As DateTime

Parameter

dateTime
DateTime

Tanggal dan waktu untuk mengonversi.

Mengembalikan

Waktu Universal Terkoordinasi (UTC) yang sesuai dengan dateTime parameter . Properti DateTime nilai Kind selalu diatur ke Utc.

Pengecualian

TimeZoneInfo.Local.IsInvalidDateTime( dateTime ) mengembalikan true.

Contoh

Contoh berikut mengilustrasikan konversi nilai waktu yang propertinya Kind masing-masing adalah DateTimeKind.Utc, DateTimeKind.Local, dan DateTimeKind.Unspecified, . Ini juga menggambarkan konversi waktu ambigu dan tidak valid.

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
//
let datNowLocal = DateTime.Now
printfn $"Converting {datNowLocal}, Kind {datNowLocal.Kind}:"
printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datNowLocal}, Kind {TimeZoneInfo.ConvertTimeToUtc(datNowLocal).Kind}\n"

let datNowUtc = DateTime.UtcNow
printfn $"Converting {datNowUtc}, Kind {datNowUtc.Kind}"
printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datNowUtc}, Kind {TimeZoneInfo.ConvertTimeToUtc(datNowUtc).Kind}\n"

let datNow = DateTime(2007, 10, 26, 13, 32, 00)
printfn $"Converting {datNow}, Kind {datNow.Kind}"
printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datNow}, Kind {TimeZoneInfo.ConvertTimeToUtc(datNow).Kind}\n"

let datAmbiguous = DateTime(2007, 11, 4, 1, 30, 00)
printfn $"Converting {datAmbiguous}, Kind {datAmbiguous.Kind}, Ambiguous {TimeZoneInfo.Local.IsAmbiguousTime datAmbiguous}"
printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datAmbiguous}, Kind {TimeZoneInfo.ConvertTimeToUtc(datAmbiguous).Kind}\n"

let datInvalid = DateTime(2007, 3, 11, 02, 30, 00)    
printfn $"Converting {datInvalid}, Kind {datInvalid.Kind}, Invalid {TimeZoneInfo.Local.IsInvalidTime datInvalid}"
try
    printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datInvalid}, Kind {TimeZoneInfo.ConvertTimeToUtc(datInvalid).Kind}"
with :? ArgumentException as e ->
    printfn $"   {e.GetType().Name}: Cannot convert {datInvalid} to UTC."
printfn ""

let datNearMax = DateTime(9999, 12, 31, 22, 00, 00)
printfn $"Converting {datNearMax}, Kind {datNearMax.Kind}"
printfn $"   ConvertTimeToUtc: {TimeZoneInfo.ConvertTimeToUtc datNearMax}, Kind {TimeZoneInfo.ConvertTimeToUtc(datNearMax).Kind}\n"
//
// 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
//
Dim datNowLocal As Date = Date.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()

Dim datNowUtc As Date = Date.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()

Dim datNow As Date = CDate("10/26/2007 1:32:00 PM")
Console.WriteLine("Converting {0}, Kind {1}", datNow, datNow.Kind)
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNow), TimeZoneInfo.ConvertTimeToUtc(datNow).Kind)
Console.WriteLine()

Dim datAmbiguous As Date = #11/4/2007 1:30:00AM#    
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()

Dim datInvalid As Date = #03/11/2007 2:30:00AM#    
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 e As ArgumentException
   Console.WriteLine("   {0}: Cannot convert {1} to UTC.", e.GetType().Name, datInvalid)
End Try
Console.WriteLine()

Dim datNearMax As Date = #12/31/9999 10:00:00PM#
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

Keterangan

Konversi yang tepat yang dilakukan tergantung pada nilai Kind properti dateTime parameter, seperti yang ditunjukkan tabel berikut.

Properti DateTime.Kind Konversi
DateTimeKind.Local Mengonversi dari waktu lokal ke Waktu Universal Terkoordinasi (UTC).
DateTimeKind.Unspecified Asumsikan dateTime adalah waktu lokal dan dikonversi dari waktu lokal ke UTC.
DateTimeKind.Utc Mengembalikan dateTime tidak berubah.

Jika dateTime sesuai dengan waktu lokal yang ambigu, metode ini mengasumsikan bahwa itu adalah waktu lokal standar. Jika dateTime sesuai dengan waktu lokal yang tidak valid, metode akan melempar ArgumentException.

Catatan

Jika zona waktu lokal komputer saat ini menyertakan beberapa aturan penyesuaian, kelebihan beban ConvertTimeToUtc metode ini dapat mengembalikan hasil yang berbeda dari TimeZone.ToUniversalTime metode dan DateTime.ToUniversalTime . TimeZone.ToUniversalTime selalu menerapkan aturan penyesuaian saat ini untuk konversi zona waktu, apakah terletak dalam rentang tanggalnya atau tidak dateTime . Dan saat mengeksekusi pada .NET Framework 3.5, DateTime.ToUniversalTime juga menerapkan aturan penyesuaian saat ini ke konversi zona waktu, apakah terletak dalam rentang tanggalnya atau tidakdateTime.

Jika setara dateTime dengan UTC lebih awal dari atau lebih DateTime.MinValue baru, DateTime.MaxValuemetode ini mengembalikan MinValue atau MaxValue, masing-masing.

Lihat juga

Berlaku untuk

ConvertTimeToUtc(DateTime, TimeZoneInfo)

Sumber:
TimeZoneInfo.cs
Sumber:
TimeZoneInfo.cs
Sumber:
TimeZoneInfo.cs

Mengonversi waktu dalam zona waktu tertentu menjadi Waktu Universal Terkoordinasi (UTC).

public:
 static DateTime ConvertTimeToUtc(DateTime dateTime, TimeZoneInfo ^ sourceTimeZone);
public static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone);
static member ConvertTimeToUtc : DateTime * TimeZoneInfo -> DateTime
Public Shared Function ConvertTimeToUtc (dateTime As DateTime, sourceTimeZone As TimeZoneInfo) As DateTime

Parameter

dateTime
DateTime

Tanggal dan waktu untuk mengonversi.

sourceTimeZone
TimeZoneInfo

Zona dateTimewaktu .

Mengembalikan

Waktu Universal Terkoordinasi (UTC) yang sesuai dengan dateTime parameter . Properti DateTime objek Kind selalu diatur ke Utc.

Pengecualian

dateTime. Kind adalah Utc dan sourceTimeZone tidak sama dengan Utc.

-atau-

dateTime. Kind adalah Local dan sourceTimeZone tidak sama dengan Local.

-atau-

sourceTimeZone .IsInvalidDateTime( dateTime ) mengembalikan true.

sourceTimeZoneadalah null.

Contoh

Contoh berikut mengambil tanggal saat ini dari sistem lokal dan mengonversinya menjadi Waktu Universal Terkoordinasi (UTC), lalu mengonversinya ke Waktu Standar Tokyo, dan akhirnya dikonversi dari Waktu Standar Tokyo kembali ke UTC. Perhatikan bahwa dua waktu UTC identik.

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
open System

// Get time in local time zone 
let thisTime = DateTime.Now
printfn $"Time in {if TimeZoneInfo.Local.IsDaylightSavingTime thisTime then TimeZoneInfo.Local.DaylightName else TimeZoneInfo.Local.StandardName} zone: {thisTime}"
printfn $"   UTC Time: {TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local)}"
// Get Tokyo Standard Time zone
let tst = TimeZoneInfo.FindSystemTimeZoneById "Tokyo Standard Time"
let tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
printfn $"Time in {if tst.IsDaylightSavingTime tstTime then tst.DaylightName else tst.StandardName} zone: {tstTime}"
printfn $"   UTC Time: {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
Module Example
   Public Sub Main()
      ' Get time in local time zone 
      Dim thisTime As Date = Date.Now
      Console.WriteLine("Time in {0} zone: {1}", IIf(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
      Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")
      Dim tstTime As Date = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
      Console.WriteLine("Time in {0} zone: {1}", IIf(tst.IsDaylightSavingTime(tstTime), 
                        tst.DaylightName, tst.StandardName), tstTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst))
   End Sub
End Module
' 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

Keterangan

Kind Jika properti dateTime parameter sama DateTimeKind.Utc dengan dan sourceTimeZone parameter sama dengan TimeZoneInfo.Utc, metode ini mengembalikan dateTime tanpa melakukan konversi apa pun.

Jika dateTime sesuai dengan waktu yang ambigu, metode ini mengasumsikan bahwa itu adalah waktu standar zona waktu sumber. Jika dateTime sesuai dengan waktu yang tidak valid, metode ini akan melempar ArgumentException.

Jika Waktu Universal Terkoordinasi (UTC) yang setara dateTime lebih awal dari atau lebih DateTime.MinValue baru, DateTime.MaxValuemetode ini masing-masing mengembalikan MinValue atau MaxValue, .

Lihat juga

Berlaku untuk