C# Convert ToString(DateTime, IFormatProvider)

Description

Convert ToString(DateTime, IFormatProvider) converts the value of the specified DateTime to its equivalent string representation, using the specified culture-specific formatting information.

Syntax

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


public static string ToString(
  DateTime value,
  IFormatProvider provider
)

Parameters

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

  • value - The date and time value to convert.
  • provider - An object that supplies culture-specific formatting information.

Returns

Convert.ToString(DateTime, IFormatProvider) method returns The string representation of value.

Example

The following example converts a DateTime value to its equivalent string representation in eight different cultures.


/*from   ww  w  . ja  v  a 2 s .  com*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTime tDate = new DateTime(2010, 4, 15, 20, 30, 40, 333);
    string[] cultureNames = { "en-US", "es-AR", "fr-FR", "hi-IN",
                              "ja-JP", "nl-NL", "ru-RU", "ur-PK" };
    
    Console.WriteLine("Converting the date {0}: ", 
                      Convert.ToString(tDate, 
                              System.Globalization.CultureInfo.InvariantCulture));
    
    foreach (string cultureName in cultureNames)
    {
       System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
       string dateString = Convert.ToString(tDate, culture);
       Console.WriteLine("   {0}:  {1,-12}", 
                         culture.Name, dateString);
    }
  }
}
    

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