DateTime.ToUniversalTime Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengonversi nilai objek saat ini DateTime menjadi Waktu Universal Terkoordinasi (UTC).
public:
DateTime ToUniversalTime();
public DateTime ToUniversalTime ();
member this.ToUniversalTime : unit -> DateTime
Public Function ToUniversalTime () As DateTime
Mengembalikan
Objek yang propertinya Kind adalah Utc, dan yang nilainya adalah UTC yang setara dengan nilai objek saat ini DateTime , atau DateTime.MaxValue jika nilai yang dikonversi terlalu besar untuk diwakili oleh DateTime objek, atau DateTime.MinValue jika nilai yang dikonversi terlalu kecil untuk diwakili oleh DateTime objek.
Contoh
Contoh berikut menunjukkan ToUniversalTime metode .
using namespace System;
void main()
{
Console::WriteLine("Enter a date and time.");
String^ strDateTime = Console::ReadLine();
DateTime localDateTime, univDateTime;
try
{
localDateTime = DateTime::Parse(strDateTime);
univDateTime = localDateTime.ToUniversalTime();
Console::WriteLine("{0} local time is {1} universal time.",
localDateTime, univDateTime );
}
catch (FormatException^)
{
Console::WriteLine("Invalid format.");
return;
}
Console::WriteLine("Enter a date and time in universal time.");
strDateTime = Console::ReadLine();
try
{
univDateTime = DateTime::Parse(strDateTime);
localDateTime = univDateTime.ToLocalTime();
Console::WriteLine("{0} universal time is {1} local time.",
univDateTime, localDateTime );
}
catch (FormatException^)
{
Console::WriteLine("Invalid format.");
return;
}
}
// The example displays output like the following when run on a
// computer whose culture is en-US in the Pacific Standard Time zone:
// Enter a date and time.
// 12/10/2015 6:18 AM
// 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
// Enter a date and time in universal time.
// 12/20/2015 6:42:00
// 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
open System
printfn "Enter a date and time."
try
let strDateTime = stdin.ReadLine()
let localDateTime = DateTime.Parse strDateTime
let univDateTime = localDateTime.ToUniversalTime()
printfn $"{localDateTime} local time is {univDateTime} universal time."
with :? FormatException ->
printfn "Invalid format."
printfn "Enter a date and time in universal time."
try
let strDateTime = stdin.ReadLine()
let univDateTime = DateTime.Parse strDateTime
let localDateTime = univDateTime.ToLocalTime()
printfn $"{univDateTime} universal time is {localDateTime} local time."
with :? FormatException ->
printfn "Invalid format."
// The example displays output like the following when run on a
// computer whose culture is en-US in the Pacific Standard Time zone:
// Enter a date and time.
// 12/10/2015 6:18 AM
// 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
// Enter a date and time in universal time.
// 12/20/2015 6:42:00
// 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
using System;
class Example
{
static void Main()
{
DateTime localDateTime, univDateTime;
Console.WriteLine("Enter a date and time.");
string strDateTime = Console.ReadLine();
try {
localDateTime = DateTime.Parse(strDateTime);
univDateTime = localDateTime.ToUniversalTime();
Console.WriteLine("{0} local time is {1} universal time.",
localDateTime,
univDateTime);
}
catch (FormatException) {
Console.WriteLine("Invalid format.");
return;
}
Console.WriteLine("Enter a date and time in universal time.");
strDateTime = Console.ReadLine();
try {
univDateTime = DateTime.Parse(strDateTime);
localDateTime = univDateTime.ToLocalTime();
Console.WriteLine("{0} universal time is {1} local time.",
univDateTime,
localDateTime);
}
catch (FormatException) {
Console.WriteLine("Invalid format.");
return;
}
}
}
// The example displays output like the following when run on a
// computer whose culture is en-US in the Pacific Standard Time zone:
// Enter a date and time.
// 12/10/2015 6:18 AM
// 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
// Enter a date and time in universal time.
// 12/20/2015 6:42:00
// 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
Module Example
Sub Main()
Dim localDateTime, univDateTime As DateTime
Console.WriteLine("Enter a date and time.")
Dim strDateTime As String = Console.ReadLine()
Try
localDateTime = DateTime.Parse(strDateTime)
univDateTime = localDateTime.ToUniversalTime()
Console.WriteLine("{0} local time is {1} universal time.",
localDateTime, univDateTime)
Catch exp As FormatException
Console.WriteLine("Invalid format.")
End Try
Console.WriteLine("Enter a date and time in universal time.")
strDateTime = Console.ReadLine()
Try
univDateTime = DateTime.Parse(strDateTime)
localDateTime = univDateTime.ToLocalTime()
Console.WriteLine("{0} universal time is {1} local time.", _
univDateTime, localDateTime)
Catch exp As FormatException
Console.WriteLine("Invalid format.")
End Try
End Sub
End Module
' The example displays output like the following when run on a
' computer whose culture is en-US in the Pacific Standard Time zone:
' Enter a date and time.
' 12/10/2015 6:18 AM
' 12/10/2015 6:18:00 AM local time is 12/10/2015 2:18:00 PM universal time.
' Enter a date and time in universal time.
' 12/20/2015 6:42:00
' 12/20/2015 6:42:00 AM universal time is 12/19/2015 10:42:00 PM local time.
Contoh berikut menggunakan SpecifyKind metode untuk menunjukkan bagaimana Kind properti memengaruhi ToLocalTime metode konversi dan ToUniversalTime .
// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(),
// and ToUniversalTime() methods.
open System
// Display the value and Kind property of a DateTime structure, the
// DateTime structure converted to local time, and the DateTime
// structure converted to universal time.
let datePatt = @"M/d/yyyy hh:mm:ss tt"
let display title (inputDt: DateTime) =
// Display the original DateTime.
let dispDt = inputDt
let dtString = dispDt.ToString datePatt
printfn $"%s{title} {dtString}, Kind = {dispDt.Kind}"
// Convert inputDt to local time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was universal time.
let dispDt = inputDt.ToLocalTime()
let dtString = dispDt.ToString datePatt
printfn $" ToLocalTime: {dtString}, Kind = {dispDt.Kind}"
// Convert inputDt to universal time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was local time.
let dispDt = inputDt.ToUniversalTime()
let dtString = dispDt.ToString datePatt
printfn $" ToUniversalTime: {dtString}, Kind = {dispDt.Kind}\n"
// Display the value and Kind property for DateTime.Now and DateTime.UtcNow.
let displayNow title (inputDt: DateTime) =
let dtString = inputDt.ToString datePatt
printfn $"%s{title} {dtString}, Kind = {inputDt.Kind}"
[<EntryPoint>]
let main _ =
// Get the date and time for the current moment, adjusted
// to the local time zone.
let saveNow = DateTime.Now
// Get the date and time for the current moment expressed
// as coordinated universal time (UTC).
let saveUtcNow = DateTime.UtcNow
// Display the value and Kind property of the current moment
// expressed as UTC and local time.
displayNow "UtcNow: .........." saveUtcNow
displayNow "Now: ............." saveNow
printfn ""
// Change the Kind property of the current moment to
// DateTimeKind.Utc and display the result.
let myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)
display "Utc: ............." myDt
// Change the Kind property of the current moment to
// DateTimeKind.Local and display the result.
let myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local)
display "Local: ..........." myDt
// Change the Kind property of the current moment to
// DateTimeKind.Unspecified and display the result.
let myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified)
display "Unspecified: ....." myDt
0
// This code example produces the following results:
//
// UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
// Now: ............. 5/6/2005 02:34:42 PM, Kind = Local
//
// Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
// ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
// ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc
//
// Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
// ToLocalTime: 5/6/2005 02:34:42 PM, Kind = Local
// ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
//
// Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
// ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
// ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
// This code example demonstrates the DateTime Kind, Now, and
// UtcNow properties, and the SpecifyKind(), ToLocalTime(),
// and ToUniversalTime() methods.
using System;
class Sample
{
public static void Main()
{
// Get the date and time for the current moment, adjusted
// to the local time zone.
DateTime saveNow = DateTime.Now;
// Get the date and time for the current moment expressed
// as coordinated universal time (UTC).
DateTime saveUtcNow = DateTime.UtcNow;
DateTime myDt;
// Display the value and Kind property of the current moment
// expressed as UTC and local time.
DisplayNow("UtcNow: ..........", saveUtcNow);
DisplayNow("Now: .............", saveNow);
Console.WriteLine();
// Change the Kind property of the current moment to
// DateTimeKind.Utc and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);
Display("Utc: .............", myDt);
// Change the Kind property of the current moment to
// DateTimeKind.Local and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local);
Display("Local: ...........", myDt);
// Change the Kind property of the current moment to
// DateTimeKind.Unspecified and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
Display("Unspecified: .....", myDt);
}
// Display the value and Kind property of a DateTime structure, the
// DateTime structure converted to local time, and the DateTime
// structure converted to universal time.
public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
public static void Display(string title, DateTime inputDt)
{
DateTime dispDt = inputDt;
string dtString;
// Display the original DateTime.
dtString = dispDt.ToString(datePatt);
Console.WriteLine("{0} {1}, Kind = {2}",
title, dtString, dispDt.Kind);
// Convert inputDt to local time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was universal time.
dispDt = inputDt.ToLocalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToLocalTime: {0}, Kind = {1}",
dtString, dispDt.Kind);
// Convert inputDt to universal time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was local time.
dispDt = inputDt.ToUniversalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToUniversalTime: {0}, Kind = {1}",
dtString, dispDt.Kind);
Console.WriteLine();
}
// Display the value and Kind property for DateTime.Now and DateTime.UtcNow.
public static void DisplayNow(string title, DateTime inputDt)
{
string dtString = inputDt.ToString(datePatt);
Console.WriteLine("{0} {1}, Kind = {2}",
title, dtString, inputDt.Kind);
}
}
/*
This code example produces the following results:
UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
Now: ............. 5/6/2005 02:34:42 PM, Kind = Local
Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc
Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
ToLocalTime: 5/6/2005 02:34:42 PM, Kind = Local
ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
*/
' This code example demonstrates the DateTime Kind, Now, and
' UtcNow properties, and the SpecifyKind(), ToLocalTime(),
' and ToUniversalTime() methods.
Class Sample
Public Shared Sub Main()
' Get the date and time for the current moment, adjusted
' to the local time zone.
Dim saveNow As DateTime = DateTime.Now
' Get the date and time for the current moment expressed
' as coordinated universal time (UTC).
Dim saveUtcNow As DateTime = DateTime.UtcNow
Dim myDt As DateTime
' Display the value and Kind property of the current moment
' expressed as UTC and local time.
DisplayNow("UtcNow: ..........", saveUtcNow)
DisplayNow("Now: .............", saveNow)
Console.WriteLine()
' Change the Kind property of the current moment to
' DateTimeKind.Utc and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)
Display("Utc: .............", myDt)
' Change the Kind property of the current moment to
' DateTimeKind.Local and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local)
Display("Local: ...........", myDt)
' Change the Kind property of the current moment to
' DateTimeKind.Unspecified and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified)
Display("Unspecified: .....", myDt)
End Sub
' Display the value and Kind property of a DateTime structure, the
' DateTime structure converted to local time, and the DateTime
' structure converted to universal time.
Public Shared datePatt As String = "M/d/yyyy hh:mm:ss tt"
Public Shared Sub Display(ByVal title As String, ByVal inputDt As DateTime)
Dim dispDt As DateTime = inputDt
Dim dtString As String
' Display the original DateTime.
dtString = dispDt.ToString(datePatt)
Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, dispDt.Kind)
' Convert inputDt to local time and display the result.
' If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
' If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
' If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
' performed as if inputDt was universal time.
dispDt = inputDt.ToLocalTime()
dtString = dispDt.ToString(datePatt)
Console.WriteLine(" ToLocalTime: {0}, Kind = {1}", dtString, dispDt.Kind)
' Convert inputDt to universal time and display the result.
' If inputDt.Kind is DateTimeKind.Utc, the conversion is not performed.
' If inputDt.Kind is DateTimeKind.Local, the conversion is performed.
' If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
' performed as if inputDt was local time.
dispDt = inputDt.ToUniversalTime()
dtString = dispDt.ToString(datePatt)
Console.WriteLine(" ToUniversalTime: {0}, Kind = {1}", dtString, dispDt.Kind)
Console.WriteLine()
End Sub
' Display the value and Kind property for DateTime.Now and DateTime.UtcNow.
Public Shared Sub DisplayNow(ByVal title As String, ByVal inputDt As DateTime)
Dim dtString As String = inputDt.ToString(datePatt)
Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, inputDt.Kind)
End Sub
End Class
'
'This code example produces the following results:
'
'UtcNow: .......... 5/6/2005 09:34:42 PM, Kind = Utc
'Now: ............. 5/6/2005 02:34:42 PM, Kind = Local
'
'Utc: ............. 5/6/2005 02:34:42 PM, Kind = Utc
' ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
' ToUniversalTime: 5/6/2005 02:34:42 PM, Kind = Utc
'
'Local: ........... 5/6/2005 02:34:42 PM, Kind = Local
' ToLocalTime: 5/6/2005 02:34:42 PM, Kind = Local
' ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
'
'Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
' ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
' ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
'
Keterangan
Waktu Universal Terkoordinasi (UTC) sama dengan waktu lokal dikurangi offset UTC. Untuk informasi selengkapnya tentang offset UTC, lihat TimeZoneInfo.GetUtcOffset. Konversi juga memperhitungkan aturan waktu musim panas yang berlaku untuk waktu yang diwakili oleh objek saat ini DateTime .
Penting
Pada sistem Windows XP, ToUniversalTime metode ini hanya mengenali aturan penyesuaian saat ini saat mengonversi dari waktu lokal ke UTC. Akibatnya, konversi untuk periode sebelum aturan penyesuaian saat ini mulai berlaku mungkin tidak secara akurat mencerminkan perbedaan antara waktu lokal dan UTC.
Dimulai dengan .NET Framework versi 2.0, nilai yang dikembalikan oleh ToUniversalTime metode ditentukan oleh Kind properti objek saat iniDateTime. Tabel berikut ini menjelaskan kemungkinan hasilnya.
Jenis | Hasil |
---|---|
Utc | Tidak ada konversi yang dilakukan. |
Local | Objek saat ini DateTime dikonversi ke UTC. |
Unspecified | Objek saat ini DateTime diasumsikan sebagai waktu lokal, dan konversi dilakukan seolah-olah Kind adalah Local. |
Catatan
Metode mengonversi ToUniversalTimeDateTime nilai dari waktu lokal ke UTC. Untuk mengonversi waktu di zona waktu non-lokal ke UTC, gunakan metode .TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) Untuk mengonversi waktu yang offsetnya dari UTC diketahui, gunakan metode .ToUniversalTime
Jika nilai instans tanggal dan waktu adalah waktu yang ambigu, metode ini mengasumsikan bahwa itu adalah waktu standar. (Waktu ambigu adalah waktu yang dapat memetakan baik ke waktu standar atau ke waktu musim panas di zona waktu lokal) Jika nilai instans tanggal dan waktu adalah waktu yang tidak valid, metode ini hanya mengurangi waktu lokal dari offset UTC zona waktu lokal untuk mengembalikan UTC. (Waktu yang tidak valid adalah waktu yang tidak ada karena penerapan aturan penyesuaian waktu musim panas.)
Catatan Bagi Pemanggil
Metode ToUniversalTime() ini terkadang digunakan untuk mengonversi waktu lokal ke UTC. Metode ToLocalTime() ini kemudian dipanggil untuk memulihkan waktu lokal asli. Namun, jika waktu asli mewakili waktu yang tidak valid di zona waktu lokal, dua nilai waktu lokal tidak akan sama. Untuk informasi tambahan dan contohnya ToLocalTime() , lihat metode .
Pada sistem Windows XP, ToUniversalTime() metode ini hanya mengenali aturan penyesuaian saat ini untuk zona waktu lokal, yang berlaku untuk semua tanggal, termasuk tanggal tingkat bawah (yaitu, tanggal yang lebih awal dari tanggal awal aturan penyesuaian saat ini). Aplikasi yang berjalan pada Windows XP yang memerlukan perhitungan tanggal dan waktu lokal yang akurat secara historis harus mengatasi perilaku ini dengan menggunakan FindSystemTimeZoneById(String) metode untuk mengambil TimeZoneInfo objek yang sesuai dengan zona waktu lokal dan memanggil metodenya ConvertTimeToUtc(DateTime, TimeZoneInfo) .
Contoh berikut mengilustrasikan perbedaan antara ToUniversalTime() metode dan ConvertTimeToUtc(DateTime, TimeZoneInfo) pada sistem Windows XP di zona Waktu Pasifik AS. Dua panggilan metode pertama menerapkan aturan penyesuaian zona waktu saat ini (yang mulai berlaku pada tahun 2007) ke tanggal di 2006. Aturan penyesuaian saat ini menyediakan transisi ke waktu musim panas pada hari Minggu kedua Maret; aturan sebelumnya, yang berlaku pada tahun 2006, asalkan transisi ke siang hari menghemat waktu terjadi pada hari Minggu pertama April. Hanya panggilan metode ketiga yang secara akurat melakukan konversi tanggal dan waktu historis ini.
using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2006, 3, 21, 2, 0, 0);
Console.WriteLine(date1.ToUniversalTime());
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1));
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, tz));
}
}
// The example displays the following output on Windows XP systems:
// 3/21/2006 9:00:00 AM
// 3/21/2006 9:00:00 AM
// 3/21/2006 10:00:00 AM
open System
let date1 = DateTime(2006, 3, 21, 2, 0, 0)
printfn $"{date1.ToUniversalTime()}"
printfn $"{TimeZoneInfo.ConvertTimeToUtc date1}"
let tz = TimeZoneInfo.FindSystemTimeZoneById "Pacific Standard Time"
printfn $"{TimeZoneInfo.ConvertTimeToUtc(date1, tz)}"
// The example displays the following output on Windows XP systems:
// 3/21/2006 9:00:00 AM
// 3/21/2006 9:00:00 AM
// 3/21/2006 10:00:00 AM
Module Example
Public Sub Main()
Dim date1 As Date = #3/21/2006 2:00AM#
Console.WriteLine(date1.ToUniversalTime())
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1))
Dim tz As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, tz))
End Sub
End Module
' The example displays the following output on Windows XP systems:
' 3/21/2006 9:00:00 AM
' 3/21/2006 9:00:00 AM
' 3/21/2006 10:00:00 AM