DateTimeOffset.ToLocalTime方法用於將當前的DateTimeOffset對象轉換為代表本地時間的DateTimeOffset對象。
用法: public DateTimeOffset ToLocalTime ();
返回值:此方法返回一個對象,該對象表示轉換為本地時間的當前DateTimeOffset對象的日期和時間。
以下示例程序旨在說明DateTimeOffset.ToLocalTime()方法的使用:
示例1:
// C# program to demonstrate the
// DateTimeOffset.ToLocalTime()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2007,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Converts the current DateTimeOffset object
// to a DateTimeOffset object that represents
// the local time instance using the
// ToLocalTime() method
DateTimeOffset value = offset.ToLocalTime();
// Display the time
Console.WriteLine("DateTimeOffset is {0}", value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
DateTimeOffset is 06/01/2007 12:55:00 +00:00
示例2:
// C# program to demonstrate the
// DateTimeOffset.ToLocalTime()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2027,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// Converts the current DateTimeOffset object
// to a DateTimeOffset object that represents
// the local time instance using the
// ToLocalTime() method
DateTimeOffset value = offset.ToLocalTime();
// Display the time
Console.WriteLine("DateTimeOffset is {0}", value);
}
}
輸出:
DateTimeOffset is 06/01/2027 12:55:00 +00:00
參考:
相關用法
- C# Uri.GetHashCode()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# TimeSpan.Add()用法及代碼示例
- C# Uri.IsWellFormedOriginalString()用法及代碼示例
- C# DateTime.Add()用法及代碼示例
- C# Uri.IsBaseOf(Uri)用法及代碼示例
- C# Uri.IsHexDigit()用法及代碼示例
- C# Random.Next()用法及代碼示例
- C# Uri.ToString()用法及代碼示例
- C# Uri.FromHex()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTimeOffset.ToLocalTime() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。