當前位置: 首頁>>代碼示例>>C#>>正文


C# DateTimeOffset構造函數代碼示例

本文整理匯總了C#中System.DateTimeOffset.DateTimeOffset構造函數的典型用法代碼示例。如果您正苦於以下問題:C# DateTimeOffset構造函數的具體用法?C# DateTimeOffset怎麽用?C# DateTimeOffset使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在System.DateTimeOffset的用法示例。


在下文中一共展示了DateTimeOffset構造函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DateTimeOffset

DateTime localNow = DateTime.Now;
DateTimeOffset localOffset = new DateTimeOffset(localNow);
Console.WriteLine(localOffset.ToString());

DateTime utcNow = DateTime.UtcNow;
DateTimeOffset utcOffset = new DateTimeOffset(utcNow);
Console.WriteLine(utcOffset.ToString());

DateTime unspecifiedNow = DateTime.SpecifyKind(DateTime.Now, 
                               DateTimeKind.Unspecified);
DateTimeOffset unspecifiedOffset = new DateTimeOffset(unspecifiedNow);
Console.WriteLine(unspecifiedOffset.ToString());
//
// The code produces the following output if run on Feb. 23, 2007, on
// a system 8 hours earlier than UTC:
//   2/23/2007 4:21:58 PM -08:00
//   2/24/2007 12:21:58 AM +00:00
//   2/23/2007 4:21:58 PM -08:00
開發者ID:.NET開發者,項目名稱:System,代碼行數:18,代碼來源:DateTimeOffset

示例2: DateTime

DateTime localTime = new DateTime(2007, 07, 12, 06, 32, 00); 
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime, 
                         TimeZoneInfo.Local.GetUtcOffset(localTime));
Console.WriteLine(dateAndOffset);
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:DateTimeOffset

輸出:

7/12/2007 6:32:00 AM -07:00

示例3: DateTime

DateTime dateWithoutOffset = new DateTime(2007, 7, 16, 13, 32, 00);
DateTimeOffset timeFromTicks = new DateTimeOffset(dateWithoutOffset.Ticks, 
                               new TimeSpan(-5, 0, 0));
Console.WriteLine(timeFromTicks.ToString());
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:DateTimeOffset

輸出:

7/16/2007 1:32:00 PM -05:00

示例4: DateTimeOffset

DateTime localTime = DateTime.Now;
DateTimeOffset nonLocalDateWithOffset = new DateTimeOffset(localTime.Ticks, 
                                  new TimeSpan(2, 0, 0));
Console.WriteLine(nonLocalDateWithOffset); 
//
// The code produces the following output if run on Feb. 23, 2007:
//    2/23/2007 4:37:50 PM +02:00
開發者ID:.NET開發者,項目名稱:System,代碼行數:7,代碼來源:DateTimeOffset

示例5: DateTime

DateTime specificDate = new DateTime(2008, 5, 1, 06, 32, 00); 
   DateTimeOffset offsetDate = new DateTimeOffset(specificDate.Year, 
                                   specificDate.Month, 
                                   specificDate.Day, 
                                   specificDate.Hour, 
                                   specificDate.Minute, 
                                   specificDate.Second, 
                                   new TimeSpan(-5, 0, 0));
   Console.WriteLine("Current time: {0}", offsetDate);
   Console.WriteLine("Corresponding UTC time: {0}", offsetDate.UtcDateTime);
開發者ID:.NET開發者,項目名稱:System,代碼行數:10,代碼來源:DateTimeOffset

輸出:

Current time: 5/1/2008 6:32:00 AM -05:00
Corresponding UTC time: 5/1/2008 11:32:00 AM

示例6: DateTime

string fmt = "dd MMM yyyy HH:mm:ss";
DateTime thisDate = new DateTime(2007, 06, 12, 19, 00, 14, 16);
DateTimeOffset offsetDate = new DateTimeOffset(thisDate.Year, 
                                               thisDate.Month, 
                                               thisDate.Day, 
                                               thisDate.Hour,
                                               thisDate.Minute,
                                               thisDate.Second,
                                               thisDate.Millisecond, 
                                               new TimeSpan(2, 0, 0));  
Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), offsetDate.Millisecond);
開發者ID:.NET開發者,項目名稱:System,代碼行數:11,代碼來源:DateTimeOffset

輸出:

Current time: 12 Jun 2007 19:00:14:16

示例7: HebrewCalendar

CultureInfo fmt;
int year; 
Calendar cal;
DateTimeOffset dateInCal;

// Instantiate DateTimeOffset with Hebrew calendar
year = 5770;
cal = new HebrewCalendar();
fmt = new CultureInfo("he-IL");
fmt.DateTimeFormat.Calendar = cal;      
dateInCal = new DateTimeOffset(year, 7, 12, 
                               15, 30, 0, 0, 
                               cal, 
                               new TimeSpan(2, 0, 0));
// Display the date in the Hebrew calendar
Console.WriteLine("Date in Hebrew Calendar: {0:g}", 
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();

// Instantiate DateTimeOffset with Hijri calendar
year = 1431;
cal = new HijriCalendar();
fmt = new CultureInfo("ar-SA");
fmt.DateTimeFormat.Calendar = cal;
dateInCal = new DateTimeOffset(year, 7, 12, 
                               15, 30, 0, 0, 
                               cal, 
                               new TimeSpan(2, 0, 0));
// Display the date in the Hijri calendar
Console.WriteLine("Date in Hijri Calendar: {0:g}", 
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();
開發者ID:.NET開發者,項目名稱:System,代碼行數:36,代碼來源:DateTimeOffset


注:本文中的System.DateTimeOffset.DateTimeOffset構造函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。