Share via


DateTimeOffset.ToString Method (String)

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.

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

Syntax

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

Parameters

Return Value

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

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 the current culture.

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) that defines the format of the returned string. If format is a null or empty string (""), the DateTimeOffset value 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 object 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.

This method uses formatting information derived from the current culture. For more information, see CurrentCulture.

Notes to Callers

The ToString(String) method returns the string representation of the date and time in the calendar used by the current culture. 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 HebrewCalendar class when the current culture is Hebrew (Israel).

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim date1 As New DateTimeOffset(#7/21/1550#, TimeSpan.Zero)
      Dim dft As CultureInfo
      Dim heIL As 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
         outputBlock.Text &= date1.ToString("G") & vbCrLf
      Catch e As ArgumentOutOfRangeException
         outputBlock.Text += String.Format("{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)) & vbCrLf
      End Try

      ' Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft
   End Sub
End Module
' The example displays the following output:
'    07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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
      {
         outputBlock.Text += date1.ToString("G") + "\n";
      }
      catch (ArgumentOutOfRangeException)
      {
         outputBlock.Text += String.Format("{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)) + "\n";
      }

      // Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft;
   }
}
// The example displays the following output:
//    07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239

Examples

The following example displays a DateTimeOffset object to the console using each of the standard date and time format specifiers. The output is formatted by using the en-us culture.

Dim outputDate As New DateTimeOffset(#10/31/2007 9:00:00 PM#, _
                                     New TimeSpan(-8, 0, 0))
Dim specifier As String

' Output date using each standard date/time format specifier
specifier = "d"
' Displays   d: 10/31/2007
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "D"
' Displays   D: Wednesday, October 31, 2007
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "t"
' Displays   t: 9:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "T"
' Displays   T: 9:00:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "f"
' Displays   f: Wednesday, October 31, 2007 9:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "F"
' Displays   F: Wednesday, October 31, 2007 9:00:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "g"
' Displays   g: 10/31/2007 9:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "G"
' Displays   G: 10/31/2007 9:00:00 PM
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "M"           ' 'm' is identical
' Displays   M: October 31
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "R"           ' 'r' is identical
' Displays   R: Thu, 01 Nov 2007 05:00:00 GMT
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "s"
' Displays   s: 2007-10-31T21:00:00
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

specifier = "u"
' Displays   u: 2007-11-01 05:00:00Z
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf

' Specifier is not supported
specifier = "U"
Try
   outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf
Catch e As FormatException
   outputBlock.Text &= String.Format("{0}: Not supported.", specifier) & vbCrLf
End Try

specifier = "Y"         ' 'y' is identical
' Displays   Y: October, 2007
outputBlock.Text &= String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) & vbCrLf
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
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "D";
// Displays   D: Wednesday, October 31, 2007
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "t";
// Displays   t: 9:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "T";
// Displays   T: 9:00:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "f";
// Displays   f: Wednesday, October 31, 2007 9:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "F";
// Displays   F: Wednesday, October 31, 2007 9:00:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "g";
// Displays   g: 10/31/2007 9:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "G";
// Displays   G: 10/31/2007 9:00:00 PM
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "M";           // 'm' is identical
// Displays   M: October 31
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "R";           // 'r' is identical
// Displays   R: Thu, 01 Nov 2007 05:00:00 GMT
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "s";
// Displays   s: 2007-10-31T21:00:00
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

specifier = "u";
// Displays   u: 2007-11-01 05:00:00Z
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

// Specifier is not supported
specifier = "U";
try
{
   outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0}: Not supported.", specifier) + "\n";
}

specifier = "Y";         // 'y' is identical
// Displays   Y: October, 2007
outputBlock.Text += String.Format("{0}: {1}", specifier, outputDate.ToString(specifier)) + "\n";

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.