Freigeben über


DateTimeOffset.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format and culture-specific format information.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function ToString ( _
    format As String, _
    formatProvider As IFormatProvider _
) As String
public string ToString(
    string format,
    IFormatProvider formatProvider
)

Parameters

  • formatProvider
    Type: System.IFormatProvider
    An object that supplies culture-specific formatting information.

Return Value

Type: System.String
A string representation of the value of the current DateTimeOffset object, as specified by format and provider.

Implements

IFormattable.ToString(String, IFormatProvider)

Exceptions

Exception Condition
FormatException

The length of format is one, and it is not one of the standard format specifier characters defined for DateTimeFormatInfo.

-or-

format does not contain a valid custom format pattern.

ArgumentOutOfRangeException

The date and time is outside the range of dates supported by the calendar used by formatProvider.

Remarks

The format parameter should contain either a single format specifier character (see Standard Date and Time Format Strings) or a custom format pattern (see Custom Date and Time Format Strings). If format is a null or empty string (""), the DateTimeOffset object is output using the default format.

The following table shows the exact operation of certain format specifiers when used with DateTimeOffset, which differs from their behavior when used with DateTime.

Existing format specifier

New behavior

"K"

Designed to round-trip a date and time. With DateTimeOffset, maps to "zzz" (the offset is always displayed with hours and minutes). Note that "K" is a custom format specifier; it cannot appear as the single character in format.

"U"

Not supported.

"r"

Converts the DateTimeOffset object to Coordinated Universal Time (UTC) and outputs it using the custom format string ddd, dd MMM yyyy HH:mm:ss GMT.

"u"

Converts the DateTimeOffset value to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ.

The remaining standard date and time format specifiers behave the same with the ToString(String) method as they do with the ToString method.

The pattern that corresponds to standard format specifiers, as well as the symbols and names of date and time components, is defined by the formatProvider parameter. The formatProvider parameter can be either of the following:

If formatProvider is nulla null reference (Nothing in Visual Basic), the DateTimeFormatInfo object associated with the current culture is used (see CurrentCulture).

Notes to Callers

The ToString(String, IFormatProvider) method returns the string representation of the date and time in the calendar used by the formatProvider parameter. Its calendar is defined by the Calendar property. If the value of the current DateTimeOffset instance is earlier than Calendar.MinSupportedDateTime or later than Calendar.MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. The following example provides an illustration. It attempts to format a date that is outside the range of the UmAlQuraCalendar class.

Imports System.Globalization

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim arSA As New CultureInfo("ar-SA")
      arSA.DateTimeFormat.Calendar = New UmAlQuraCalendar()
      Dim date1 As New DateTimeOffset(#9/10/1890#, TimeSpan.Zero)

      Try
         outputBlock.Text &= date1.ToString("d", arSA) & vbCrLf
      Catch e As ArgumentOutOfRangeException
         outputBlock.Text += String.Format("{0:d} is earlier than {1:d} or later than {2:d}", _
                           date1, _
                           arSA.DateTimeFormat.Calendar.MinSupportedDateTime, _
                           arSA.DateTimeFormat.Calendar.MaxSupportedDateTime) & vbCrLf
      End Try
   End Sub
End Module
' The example displays the following output:
'    9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      CultureInfo arSA = new CultureInfo("ar-SA");
      arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar();
      DateTimeOffset date1 = new DateTimeOffset(new DateTime(1890, 9, 10),
                                                TimeSpan.Zero);

      try
      {
         outputBlock.Text += date1.ToString("d", arSA) + "\n";
      }
      catch (ArgumentOutOfRangeException)
      {
         outputBlock.Text += String.Format("{0:d} is earlier than {1:d} or later than {2:d}",
                           date1,
                           arSA.DateTimeFormat.Calendar.MinSupportedDateTime,
                           arSA.DateTimeFormat.Calendar.MaxSupportedDateTime) + "\n";
      }
   }
}
// The example displays the following output:
//    9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029

Examples

The following example uses the ToString(String, IFormatProvider) method to display a DateTimeOffset object using a custom format string for several different cultures.

Dim outputDate As New DateTimeOffset(#11/1/2007 9:00:00 AM#, _
                                     New TimeSpan(-7, 0, 0))
Dim format As String = "dddd, MMM dd yyyy HH:mm:ss zzz"

' Output date and time using custom format specification
outputBlock.Text &= String.Format(outputDate.ToString(format, Nothing)) & vbCrLf
outputBlock.Text &= String.Format(outputDate.ToString(format, CultureInfo.InvariantCulture)) & vbCrLf
outputBlock.Text &= String.Format(outputDate.ToString(format, _
                                      New CultureInfo("fr-FR"))) & vbCrLf
outputBlock.Text &= String.Format(outputDate.ToString(format, _
                                      New CultureInfo("es-ES"))) & vbCrLf
' The example displays the following output:
'    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
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
outputBlock.Text += String.Format(outputDate.ToString(format, null as DateTimeFormatInfo)) + "\n";
outputBlock.Text += String.Format(outputDate.ToString(format, CultureInfo.InvariantCulture)) + "\n";
outputBlock.Text += String.Format(outputDate.ToString(format,
                                      new CultureInfo("fr-FR"))) + "\n";
outputBlock.Text += String.Format(outputDate.ToString(format,
                                      new CultureInfo("es-ES"))) + "\n";
// The example displays the following output:
//    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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.