DateTime.ToString Method (String, IFormatProvider)
Assembly: mscorlib (in mscorlib.dll)
public final String ToString ( String format, IFormatProvider provider )
public final function ToString ( format : String, provider : IFormatProvider ) : String
Parameters
- format
A format string.
- provider
An IFormatProvider that supplies culture-specific formatting information.
Return Value
A string representation of value of this instance as specified by format and provider.| Exception type | Condition |
|---|---|
| The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo. -or- format does not contain a valid custom format pattern. |
The format parameter can contain either a format specifier character or a custom format pattern. For more information, see the summary page for System.Globalization.DateTimeFormatInfo.
If format is a null reference (Nothing in Visual Basic) or an empty string (""), the standard format specifier, 'G', is used.
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 namespace System; using namespace System::Globalization; void main() { DateTime dt = DateTime::Now; array<String^>^format = {L"d",L"D",L"f",L"F",L"g",L"G",L"m",L"r",L"s",L"t",L"T",L"u",L"U",L"y",L"dddd, MMMM dd yyyy",L"ddd, MMM d \"'\"yy",L"dddd, MMMM dd",L"M/yy",L"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 ], L" :", 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.