Export (0) Print
Expand All

DateTime.ToString Method (IFormatProvider)

Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string ToString (
	IFormatProvider provider
)
public final String ToString (
	IFormatProvider provider
)
public final function ToString (
	provider : IFormatProvider
) : String

Parameters

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value

A string representation of value of this instance as specified by provider.

This instance is formatted with the general format specifier, 'G', as described in the Formatting Overview topic. The return value is identical to the value returned by ToString ("G", null).

The provider parameter is typically a DateTimeFormatInfo object or an instance of CultureInfo, which contains a DateTimeFormatInfo object. If provider is a null reference (Nothing in Visual Basic), the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.

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

 using System;
 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));
        }
  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

import System.*;
import System.Globalization.*;

public class MainClass
{
    public static void main(String[] args)
    {
        DateTime dT = DateTime.get_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.get_Length(); i++) {
            date = dT.ToString(format[i], DateTimeFormatInfo.get_InvariantInfo());
            Console.WriteLine(String.Concat(format[i], " :", date));
        }
    } //main 
} //MainClass 

 /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */

import System;
import System.Globalization; 
 

var dt : DateTime = DateTime.Now;
var format : String[] = [
    "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",
    ];
var date : String;
for (var i : int = 0; i < format.Length; i++) {
    date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
    Console.WriteLine(format[i] + " :" + date);
}

  
   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft