本文整理汇总了C#中System.DateTimeOffset.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeOffset.ToString方法的具体用法?C# DateTimeOffset.ToString怎么用?C# DateTimeOffset.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.DateTimeOffset
的用法示例。
在下文中一共展示了DateTimeOffset.ToString方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TimeSpan
DateTimeOffset thisDate;
// Show output for UTC time
thisDate = DateTimeOffset.UtcNow;
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 7:13:50 PM +00:00
// Show output for local time
thisDate = DateTimeOffset.Now;
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 12:13:50 PM -07:00
// Show output for arbitrary time offset
thisDate = thisDate.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 2:13:50 PM -05:00
示例2: Main
//引入命名空间
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTimeOffset date1 = new DateTimeOffset(new DateTime(550, 1, 1),
TimeSpan.Zero);
CultureInfo dft;
CultureInfo arSY = new CultureInfo("ar-SY");
arSY.DateTimeFormat.Calendar = new HijriCalendar();
// Change current culture to ar-SY.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = arSY;
// Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString());
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
}
// Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
输出:
01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999
示例3: CultureInfo
CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture,
new CultureInfo("en-us"),
new CultureInfo("fr-fr"),
new CultureInfo("de-DE"),
new CultureInfo("es-ES")};
DateTimeOffset thisDate = new DateTimeOffset(2007, 5, 1, 9, 0, 0,
TimeSpan.Zero);
foreach (CultureInfo culture in cultures)
{
string cultureName;
if (string.IsNullOrEmpty(culture.Name))
cultureName = culture.NativeName;
else
cultureName = culture.Name;
Console.WriteLine("In {0}, {1}",
cultureName, thisDate.ToString(culture));
}
// The example produces the following output:
// In Invariant Language (Invariant Country), 05/01/2007 09:00:00 +00:00
// In en-US, 5/1/2007 9:00:00 AM +00:00
// In fr-FR, 01/05/2007 09:00:00 +00:00
// In de-DE, 01.05.2007 09:00:00 +00:00
// In es-ES, 01/05/2007 9:00:00 +00:00
示例4: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo jaJP = new CultureInfo("ja-JP");
jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1867, 1, 1),
TimeSpan.Zero);
try {
Console.WriteLine(date1.ToString(jaJP));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
date1,
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
}
}
输出:
1/1/1867 is earlier than 9/8/1868 or later than 12/31/9999 }
示例5: DateTimeOffset
DateTimeOffset outputDate = new DateTimeOffset(2007, 10, 31, 21, 0, 0,
new TimeSpan(-8, 0, 0));
string specifier;
// Output date using each standard date/time format specifier
specifier = "d";
// Displays d: 10/31/2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "D";
// Displays D: Wednesday, October 31, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "t";
// Displays t: 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "T";
// Displays T: 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "f";
// Displays f: Wednesday, October 31, 2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "F";
// Displays F: Wednesday, October 31, 2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "g";
// Displays g: 10/31/2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "G";
// Displays G: 10/31/2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "M"; // 'm' is identical
// Displays M: October 31
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "R"; // 'r' is identical
// Displays R: Thu, 01 Nov 2007 05:00:00 GMT
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "s";
// Displays s: 2007-10-31T21:00:00
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "u";
// Displays u: 2007-11-01 05:00:00Z
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
// Specifier is not supported
specifier = "U";
try
{
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
}
catch (FormatException)
{
Console.WriteLine("{0}: Not supported.", specifier);
}
specifier = "Y"; // 'y' is identical
// Displays Y: October, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
示例6: Main
//引入命名空间
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1550, 7, 21),
TimeSpan.Zero);
CultureInfo dft;
CultureInfo heIL = new CultureInfo("he-IL");
heIL.DateTimeFormat.Calendar = new HebrewCalendar();
// Change current culture to he-IL.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = heIL;
// Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString("G"));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
}
// Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
输出:
07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
示例7: DateTimeOffset
DateTimeOffset outputDate = new DateTimeOffset(2007, 11, 1, 9, 0, 0,
new TimeSpan(-7, 0, 0));
string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
// Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("fr-FR")));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("es-ES")));
输出:
Thursday, Nov 01 2007 09:00:00 -07:00 Thursday, Nov 01 2007 09:00:00 -07:00 jeudi, nov. 01 2007 09:00:00 -07:00 jueves, nov 01 2007 09:00:00 -07:00
示例8: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo arSA = new CultureInfo("ar-SA");
arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar();
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1890, 9, 10),
TimeSpan.Zero);
try {
Console.WriteLine(date1.ToString("d", arSA));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
date1,
arSA.DateTimeFormat.Calendar.MinSupportedDateTime,
arSA.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
}
}
输出:
9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029