DateTime.ToLocalTime メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の DateTime オブジェクトの値を現地時刻に変換します。
public:
DateTime ToLocalTime();
public DateTime ToLocalTime ();
member this.ToLocalTime : unit -> DateTime
Public Function ToLocalTime () As DateTime
戻り値
プロパティが Kind でLocal、値が現在DateTimeのオブジェクトの値と等価なローカル時刻であるオブジェクト。変換後の値が大きすぎてオブジェクトでDateTime表すには DateTime.MaxValue、変換後の値が小さすぎてオブジェクトとしてDateTime表現できない場合は DateTime.MinValue。
例
ToLocalTimeメソッドの例を次に示します。 正確な出力は、現在のカルチャと、それが実行されているシステムのローカル タイム ゾーンによって異なります。
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.
次の例では、 メソッドをSpecifyKind使用して、 プロパティが Kind および ToUniversalTime 変換メソッドにどのように影響ToLocalTimeするかを示します。
// 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
'
注釈
現地時刻は、協定世界時 (UTC) 時刻と UTC オフセットと等しくなります。 UTC オフセットの詳細については、「」を参照してください TimeZoneInfo.GetUtcOffset。 変換では、現在 DateTime の オブジェクトによって表される時刻に適用される夏時間ルールも考慮されます。
重要
Windows XP システムでは、 メソッドは ToLocalTime UTC から現地時刻に変換するときに、現在の調整規則のみを認識します。 その結果、現在の調整規則が有効になる前の期間の変換では、UTC と現地時刻の違いが正確に反映されない可能性があります。
.NET Framework バージョン 2.0 以降では、 メソッドによってToLocalTime返される値は、現在DateTimeの オブジェクトの プロパティによってKind決まります。 次の表では、考えられる結果について説明します。
種類 | 結果 |
---|---|
Utc | のこのインスタンス DateTime は、ローカル時刻に変換されます。 |
Local | 変換は実行されません。 |
Unspecified | のこのインスタンスDateTimeは UTC 時刻であると見なされ、変換は のように実行KindUtcされます。 |
注意
メソッドは ToLocalTime 、値を DateTime UTC から現地時刻に変換します。 指定されたタイム ゾーンの時刻を現地時刻に変換するには、 メソッドを使用します TimeZoneInfo.ConvertTime 。
変換によって返される値は、 DateTime プロパティが Kind 常に を返す Localです。 したがって、 が同じ DateTimeに繰り返し適用されている場合ToLocalTimeでも、有効な結果が返されます。
注意 (呼び出し元)
メソッドをToLocalTime()使用して、 メソッドまたは FromFileTimeUtc(Int64) メソッドによって UTC に変換されたローカルの日付と時刻の値をToUniversalTime()復元できます。 ただし、元の時刻がローカル タイム ゾーンの無効な時刻を表している場合、復元された値と一致しません。 メソッドは ToLocalTime() 、UTC からローカル タイム ゾーンに時刻を変換するときに、ローカル タイム ゾーンで有効になるように時刻も調整します。
たとえば、標準時から夏時間への切り替えは、2010 年 3 月 14 日の米国太平洋標準時ゾーンで、時刻が 1 時間進んだ午前 2 時から午前 3 時に行われます。この時間間隔は無効な時間です。つまり、このタイム ゾーンに存在しない時間間隔です。 次の例は、この範囲内の時刻が メソッドによって UTC に変換され、 メソッドによってToUniversalTime()ToLocalTime()復元された場合、元の値が有効な時刻になるように調整されることを示しています。 例に示すように、 メソッドに渡 IsInvalidTime(DateTime) すことで、特定の日付と時刻の値が変更の対象になる可能性があるかどうかを判断できます。
using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local);
Console.WriteLine("Invalid time: {0}",
TimeZoneInfo.Local.IsInvalidTime(date1));
DateTime utcDate1 = date1.ToUniversalTime();
DateTime date2 = utcDate1.ToLocalTime();
Console.WriteLine("{0} --> {1}", date1, date2);
}
}
// The example displays the following output:
// Invalid time: True
// 3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM
open System
let date1 = DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local)
printfn $"Invalid time: {TimeZoneInfo.Local.IsInvalidTime date1}"
let utcDate1 = date1.ToUniversalTime()
let date2 = utcDate1.ToLocalTime()
printfn $"{date1} --> {date2}"
// The example displays the following output:
// Invalid time: True
// 3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM
Module Example
Public Sub Main()
Dim date1 As New Date(2010, 3, 14, 2, 30, 0, DateTimeKind.Local)
Console.WriteLine("Invalid time: {0}", _
TimeZoneInfo.Local.IsInvalidTime(date1))
Dim utcDate1 As Date = date1.ToUniversalTime()
Dim date2 As Date = utcDate1.ToLocalTime()
Console.WriteLine("{0} --> {1}", date1, date2)
End Sub
End Module
' The example displays the following output:
' Invalid time: True
' 3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM
適用対象
こちらもご覧ください
.NET