System.Convert.ToString 方法 (DateTime, IFormatProvider)

方法描述

使用指定的区域性特定格式设置信息,将指定 DateTime 的值转换为其等效的字符串表示形式。

语法定义(C# System.Convert.ToString 方法 (DateTime, IFormatProvider) 的用法)

public static string ToString(
	DateTime value,
	IFormatProvider provider
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-DateTime 要转换的日期和时间值。
provider System-IFormatProvider 一个提供区域性特定的格式设置信息的对象。
返回值 System.String value 的字符串表示形式。

提示和注释

此实现与 DateTime.ToString(IFormatProvider) 相同。

System.Convert.ToString 方法 (DateTime, IFormatProvider)例子

下面的示例将 DateTime 值转换为 8 种不同区域性中的等效字符串表示形式。

// Specify the date to be formatted using various cultures.
DateTime tDate = new DateTime(2010, 4, 15, 20, 30, 40, 333);
// Specify the cultures.
string[] cultureNames = { "en-US", "es-AR", "fr-FR", "hi-IN",
                          "ja-JP", "nl-NL", "ru-RU", "ur-PK" };

Console.WriteLine("Converting the date {0}: ", 
                  Convert.ToString(tDate, 
                          System.Globalization.CultureInfo.InvariantCulture));

foreach (string cultureName in cultureNames)
{
   System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
   string dateString = Convert.ToString(tDate, culture);
   Console.WriteLine("   {0}:  {1,-12}", 
                     culture.Name, dateString);
}             
// The example displays the following output:
//       Converting the date 04/15/2010 20:30:40:
//          en-US:  4/15/2010 8:30:40 PM
//          es-AR:  15/04/2010 08:30:40 p.m.
//          fr-FR:  15/04/2010 20:30:40
//          hi-IN:  15-04-2010 20:30:40
//          ja-JP:  2010/04/15 20:30:40
//          nl-NL:  15-4-2010 20:30:40
//          ru-RU:  15.04.2010 20:30:40
//          ur-PK:  15/04/2010 8:30:40 PM

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。