C# DateTime ToString(String)

Description

DateTime ToString(String) converts the value of the current DateTime object to its equivalent string representation using the specified format.

Syntax

DateTime.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

DateTime.ToString(String) has the following parameters.

  • format - A standard or custom date and time format string (see Remarks).

Returns

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

Example

The following example attempts to format a date that is outside the range of the HebrewCalendar class when the current culture is Hebrew (Israel).


using System;// ww  w.j av a  2s.  co m
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(1550, 7, 21);
      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 {
         Console.WriteLine(date1.ToString("G"));
      }   
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("{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)); 
      }

      // Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft;
   }
}

The code above generates the following result.

Example 2

The following example uses each of the standard date and time format strings and a selection of custom date and time format strings to display the string representation of a DateTime value.

The thread current culture for the example is en-US.


using System;/* w w  w .  j a  va2s. c om*/

public class DateToStringExample
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2014, 6, 15, 21, 15, 07);
      // Create an array of standard format strings. 
      string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                               "R", "s", "t", "T", "u", "U", "y"};
      // Output date and time using each standard format string. 
      foreach (string standardFmt in standardFmts)
         Console.WriteLine("{0}: {1}", standardFmt, 
                           dateValue.ToString(standardFmt));
      Console.WriteLine();

      // Create an array of some custom format strings. 
      string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
      // Output date and time using each custom format string. 
      foreach (string customFmt in customFmts)
         Console.WriteLine("'{0}': {1}", customFmt,
                           dateValue.ToString(customFmt));
   }
}

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