C# DateTime ToString(IFormatProvider)

Description

DateTime ToString(IFormatProvider) converts the value of the current DateTime object to its equivalent string representation using the specified culture-specific format information.

Syntax

DateTime.ToString(IFormatProvider) has the following syntax.


public string ToString(
  IFormatProvider provider
)

Parameters

DateTime.ToString(IFormatProvider) has the following parameters.

  • provider - An object that supplies culture-specific formatting information.

Returns

DateTime.ToString(IFormatProvider) method returns A string representation of value of the current DateTime object as specified by provider.

Example

The following example attempts to format a date that is outside the range of the JapaneseCalendar class.


/*  w ww. java2 s.  c o  m*/
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo jaJP = new CultureInfo("ja-JP");
      jaJP.DateTimeFormat.Calendar = new JapaneseCalendar(); 
      DateTime date1 = new DateTime(1867, 1, 1);

      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); 
      }
   }
}

The code above generates the following result.

Example 2

The following example displays the string representation of a date and time using CultureInfo objects that represent five different cultures.


using System;/*w  ww .  j a  v  a2s. c om*/
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture, 
                                                  new CultureInfo("en-us"), 
                                                  new CultureInfo("fr-fr"), 
                                                  new CultureInfo("de-DE"), 
                                                  new CultureInfo("es-ES"),
                                                  new CultureInfo("ja-JP")};

      DateTime thisDate = new DateTime(2014, 5, 1, 9, 0, 0);                                            

      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 code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version