Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Convert.ToString Method (DateTime, IFormatProvider)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function ToString ( _
value As DateTime, _
provider As IFormatProvider _
) As String
public static string ToString(
DateTime value,
IFormatProvider provider
)
Parameters
- value
Type: System.DateTime
A DateTime.
- provider
Type: System.IFormatProvider
An IFormatProvider interface implementation that supplies culture-specific formatting information.
Return Value
Type: System.String
The String equivalent of the value of value.
Remarks
This implementation is identical to DateTime.ToString.
Examples
The following code example converts a DateTime value to a String with the ToString method, using an IFormatProvider object.
' Example of the Convert.ToString( DateTime ) and
' Convert.ToString( DateTime, IFormatProvider ) methods.
Imports System.Globalization
Module Example
Sub DisplayDateNCultureName(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal testDate As DateTime, _
ByVal cultureName As String)
' Create the CultureInfo object for the specified culture,
' and use it as the IFormatProvider when converting the date.
Dim culture As CultureInfo = New CultureInfo(cultureName)
Dim dateString As String = _
Convert.ToString(testDate, culture)
' Bracket the culture name, and display the name and date.
outputBlock.Text &= String.Format(" {0,-12}{1}", _
String.Concat("[", cultureName, "]"), dateString) & vbCrLf
End Sub
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Specify the date to be formatted under various cultures.
Dim tDate As DateTime = _
New DateTime(2003, 4, 15, 20, 30, 40, 333)
outputBlock.Text &= String.Format("This example of " & vbCrLf & _
" Convert.ToString( DateTime ) and " & vbCrLf & _
" Convert.ToString( DateTime, IFormatProvider )" & _
vbCrLf & "generates the following output. It " & _
"creates CultureInfo objects " & vbCrLf & "for " & _
"several cultures and formats " & _
"a DateTime value with each." & vbCrLf) & vbCrLf
' Format the date without an IFormatProvider.
outputBlock.Text &= String.Format(" {0,-12}{1}", _
Nothing, "No IFormatProvider") & vbCrLf
outputBlock.Text &= String.Format(" {0,-12}{1}", _
Nothing, "------------------") & vbCrLf
outputBlock.Text &= String.Format(" {0,-12}{1}" & vbCrLf, _
String.Concat( _
"[", CultureInfo.CurrentCulture.Name, "]"), _
Convert.ToString(tDate)) & vbCrLf
' Format the date with IFormatProvider for several cultures.
outputBlock.Text &= String.Format(" {0,-12}{1}", _
"Culture", "With IFormatProvider") & vbCrLf
outputBlock.Text &= String.Format(" {0,-12}{1}", _
"-------", "--------------------") & vbCrLf
DisplayDateNCultureName(outputBlock, tDate, "")
DisplayDateNCultureName(outputBlock, tDate, "en-US")
DisplayDateNCultureName(outputBlock, tDate, "es-AR")
DisplayDateNCultureName(outputBlock, tDate, "fr-FR")
DisplayDateNCultureName(outputBlock, tDate, "hi-IN")
DisplayDateNCultureName(outputBlock, tDate, "ja-JP")
DisplayDateNCultureName(outputBlock, tDate, "nl-NL")
DisplayDateNCultureName(outputBlock, tDate, "ru-RU")
DisplayDateNCultureName(outputBlock, tDate, "ur-PK")
End Sub
End Module
' This example of
' Convert.ToString( DateTime ) and
' Convert.ToString( DateTime, IFormatProvider )
' generates the following output. It creates CultureInfo objects
' for several cultures and formats a DateTime value with each.
'
' No IFormatProvider
' ------------------
' [en-US] 4/15/2003 8:30:40 PM
'
' Culture With IFormatProvider
' ------- --------------------
' [] 04/15/2003 20:30:40
' [en-US] 4/15/2003 8:30:40 PM
' [es-AR] 15/04/2003 08:30:40 p.m.
' [fr-FR] 15/04/2003 20:30:40
' [hi-IN] 15-04-2003 20:30:40
' [ja-JP] 2003年04月15日 20:30:40
' [nl-NL] 15-4-2003 20:30:40
' [ru-RU] 15.04.2003 20:30:40
' [ur-PK] 15/04/2003 8:30:40 PM
// Example of the Convert.ToString( DateTime ) and
// Convert.ToString( DateTime, IFormatProvider ) methods.
using System;
using System.Globalization;
class Example
{
static void DisplayDateNCultureName(System.Windows.Controls.TextBlock outputBlock, DateTime testDate,
string cultureName)
{
// Create the CultureInfo object for the specified culture,
// and use it as the IFormatProvider when converting the date.
CultureInfo culture = new CultureInfo(cultureName);
string dateString = Convert.ToString(testDate, culture);
// Bracket the culture name, and display the name and date.
outputBlock.Text += String.Format(" {0,-12}{1}",
String.Concat("[", cultureName, "]"), dateString) + "\n";
}
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Specify the date to be formatted under various cultures.
DateTime tDate = new DateTime(2003, 4, 15, 20, 30, 40, 333);
outputBlock.Text += String.Format("This example of \n" +
" Convert.ToString( DateTime ) and \n" +
" Convert.ToString( DateTime, IFormatProvider )\n" +
"generates the following output. It creates " +
"CultureInfo objects \nfor several cultures " +
"and formats a DateTime value with each.\n") + "\n";
// Format the date without an IFormatProvider.
outputBlock.Text += String.Format(" {0,-12}{1}",
null, "No IFormatProvider") + "\n";
outputBlock.Text += String.Format(" {0,-12}{1}",
null, "------------------") + "\n";
outputBlock.Text += String.Format(" {0,-12}{1}\n",
String.Concat("[", CultureInfo.CurrentCulture.Name, "]"),
Convert.ToString(tDate)) + "\n";
// Format the date with IFormatProvider for several cultures.
outputBlock.Text += String.Format(" {0,-12}{1}",
"Culture", "With IFormatProvider") + "\n";
outputBlock.Text += String.Format(" {0,-12}{1}",
"-------", "--------------------") + "\n";
DisplayDateNCultureName(outputBlock, tDate, "");
DisplayDateNCultureName(outputBlock, tDate, "en-US");
DisplayDateNCultureName(outputBlock, tDate, "es-AR");
DisplayDateNCultureName(outputBlock, tDate, "fr-FR");
DisplayDateNCultureName(outputBlock, tDate, "hi-IN");
DisplayDateNCultureName(outputBlock, tDate, "ja-JP");
DisplayDateNCultureName(outputBlock, tDate, "nl-NL");
DisplayDateNCultureName(outputBlock, tDate, "ru-RU");
DisplayDateNCultureName(outputBlock, tDate, "ur-PK");
}
}
/*
This example of
Convert.ToString( DateTime ) and
Convert.ToString( DateTime, IFormatProvider )
generates the following output. It creates CultureInfo objects
for several cultures and formats a DateTime value with each.
No IFormatProvider
------------------
[en-US] 4/15/2003 8:30:40 PM
Culture With IFormatProvider
------- --------------------
[] 04/15/2003 20:30:40
[en-US] 4/15/2003 8:30:40 PM
[es-AR] 15/04/2003 08:30:40 p.m.
[fr-FR] 15/04/2003 20:30:40
[hi-IN] 15-04-2003 20:30:40
[ja-JP] 2003年04月15日 20:30:40
[nl-NL] 15-4-2003 20:30:40
[ru-RU] 15.04.2003 20:30:40
[ur-PK] 15/04/2003 8:30:40 PM
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.