Convert DateTime to string using the specified format and culture-specific format information in CSharp

Description

The following code shows how to convert DateTime to string using the specified format and culture-specific format information.

Example


using System;/*from  w w w  . j a v  a2s  . c o  m*/
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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var