C# DateTime ToString(String, IFormatProvider)

Description

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

Syntax

DateTime.ToString(String, IFormatProvider) has the following syntax.


public string ToString(
  string format,
  IFormatProvider provider
)

Parameters

DateTime.ToString(String, IFormatProvider) has the following parameters.

  • format - A standard or custom date and time format string.
  • provider - An object that supplies culture-specific formatting information.

Returns

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

Example

The following example formats a date that is outside the range of the UmAlQuraCalendar class.


/*from www  .j  a  v a2s .  com*/
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo arSA = new CultureInfo("ar-SA");
      arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar(); 
      DateTime date1 = new DateTime(1890, 9, 10);

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

The code above generates the following result.

Example 2

The following example uses each of the standard date time format strings to display the string representation of a date and time for four different cultures.


using System;//from   w ww.  j  av  a 2 s . c  o m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Create an array of all supported standard date and time format specifiers. 
      string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r", 
                          "s", "t", "T", "u", "U", "Y"};
      // Create an array of four cultures.                                 
      CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("de-DE"), 
                                CultureInfo.CreateSpecificCulture("en-US"), 
                                CultureInfo.CreateSpecificCulture("es-ES"), 
                                CultureInfo.CreateSpecificCulture("fr-FR")};
       // Define date to be displayed.
      DateTime dateToDisplay = new DateTime(2008, 10, 1, 17, 4, 32);

      // Iterate each standard format specifier. 
      foreach (string formatSpecifier in formats)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}", 
                              formatSpecifier, culture.Name, 
                              dateToDisplay.ToString(formatSpecifier, culture));
         Console.WriteLine();
      }   
   }
}

The code above generates the following result.

Example 3

The following example demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.


using System;//from  w w w .j  ava2s  . c o  m
using System.Globalization; 

public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }

    }
}

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