此方法用於將當前DateTime對象的值轉換為其等效的字符串表示形式。此方法的重載列表中共有4種方法:
- ToString(String, IFormatProvider)
- ToString(String)
- ToString(IFormatProvider)
- ToString()
在這裏,我們將隻討論前兩種方法。
ToString(String, IFormatProvider)
此方法用於使用指定的格式和區域性特定的格式信息將當前DateTime對象的值轉換為其等效的字符串表示形式。
用法: public string ToString (string format, IFormatProvider provider);
參數:
format:標準或自定義日期和時間格式字符串。
provider:提供特定於區域性的格式信息的對象。
返回值:此方法返回格式和提供程序指定的當前DateTime對象的值的字符串表示形式。
異常:
- FormatException:如果format的長度為1,並且不是為DateTimeFormatInfo定義的格式說明符之一,或者該格式不包含有效的自定義格式模式。
- ArgumentOutOfRangeException:如果日期和時間超出提供者使用的日曆支持的日期範圍。
以下示例程序旨在說明DateTime.ToString(String,IFormatProvider)方法的用法:
範例1:
// C# program to demonstrate the
// DateTime.ToString(String,
// IFormatProvider) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures =
CultureInfo.CreateSpecificCulture("de-DE");
// declaring and intializing String array
string[] format = {"d", "D", "f", "F", "g", "G",
"m", "o", "r","s", "t" };
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++)
{
get(format[j], cultures);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format,
CultureInfo cultures)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(2008, 10,
1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format, cultures);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string 01.10.2008 Mittwoch, 1. Oktober 2008 Mittwoch, 1. Oktober 2008 17:04 Mittwoch, 1. Oktober 2008 17:04:32 01.10.2008 17:04 01.10.2008 17:04:32 1. Oktober 2008-10-01T17:04:32.0000000 Wed, 01 Oct 2008 17:04:32 GMT 2008-10-01T17:04:32 17:04
範例2:對於FormatException
// C# program to demonstrate the
// DateTime.ToString(String,
// IFormatProvider) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures =
CultureInfo.CreateSpecificCulture("de-DE");
// declaring and intializing String array
string[] format = {"d", "D", "f", "F", "g", "G",
"s", "x"};
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++)
{
get(format[j], cultures);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.WriteLine("format does not contain "+
"a valid custom format pattern.");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format,
CultureInfo cultures)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(2008,
10, 1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format, cultures);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string 01.10.2008 Mittwoch, 1. Oktober 2008 Mittwoch, 1. Oktober 2008 17:04 Mittwoch, 1. Oktober 2008 17:04:32 01.10.2008 17:04 01.10.2008 17:04:32 2008-10-01T17:04:32 format does not contain a valid custom format pattern. Exception Thrown:System.FormatException
範例3:對於ArgumentOutOfRangeException
// C# program to demonstrate the
// DateTime.ToString(String,
// IFormatProvider) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures =
CultureInfo.CreateSpecificCulture("ar-SA");
// declaring and intializing String array
string[] format = {"d", "D", "f", "F",
"g", "G","s" };
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++) {
get(format[j], cultures);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.WriteLine("format does not contain "+
"a valid custom format pattern.");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.WriteLine("The date and time is outside the range of dates"
+ "supported by the calendar used by ar-SA");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format,
CultureInfo cultures)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(2999,
10, 1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format, cultures);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string The date and time is outside the range of datessupported by the calendar used by ar-SA Exception Thrown:System.ArgumentOutOfRangeException
ToString(String) Method
此方法用於使用指定的格式和當前區域性的格式約定將當前DateTime對象的值轉換為其等效的字符串表示形式。
用法: public string ToString (string format);
Here it takes a standard or custom date and time format string.
返回值:此方法返回格式表示的當前DateTime對象的值的字符串表示形式。
異常:
- FormatException:如果format的長度為1,並且不是為DateTimeFormatInfo定義的格式說明符之一,則該格式不包含有效的自定義格式模式。
- ArgumentOutOfRangeException:如果日期和時間在當前區域性使用的日曆支持的日期範圍之外。
以下示例程序旨在說明ToString(String)方法的用法:
範例1:
// C# program to demonstrate the
// DateTime.ToString(String) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and intializing String array
string[] format = {"d", "D", "f", "F", "g",
"G", "m", "o", "r","s", "t" };
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++)
{
get(format[j]);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(2008,
10, 1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string 10/01/2008 Wednesday, 01 October 2008 Wednesday, 01 October 2008 17:04 Wednesday, 01 October 2008 17:04:32 10/01/2008 17:04 10/01/2008 17:04:32 October 01 2008-10-01T17:04:32.0000000 Wed, 01 Oct 2008 17:04:32 GMT 2008-10-01T17:04:32 17:04
範例2:對於FormatException
// C# program to demonstrate the
// DateTime.ToString(String) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and intializing String array
string[] format = {"d", "D", "f", "F",
"g", "G", "s", "x" };
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++)
{
get(format[j]);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.WriteLine("format does not contain "+
"a valid custom format pattern.");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(2008,
10, 1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string 10/01/2008 Wednesday, 01 October 2008 Wednesday, 01 October 2008 17:04 Wednesday, 01 October 2008 17:04:32 10/01/2008 17:04 10/01/2008 17:04:32 2008-10-01T17:04:32 format does not contain a valid custom format pattern. Exception Thrown:System.FormatException
範例3:對於ArgumentOutOfRangeException
// C# program to demonstrate the
// DateTime.ToString(String) Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and intializing String array
string[] format = {"d", "D", "f", "F",
"g", "G","s" };
// calling get() Method
Console.WriteLine("Converts the value of the current"
+ "DateTime object to its equivalent string");
for (int j = 0; j < format.Length; j++)
{
get(format[j]);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.WriteLine("format does not contain "+
"a valid custom format pattern.");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\n");
Console.WriteLine("The date and time are outside the range of dates "
+ "supported by the calendar used by the current culture.");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string format)
{
// Define date to be displayed.
DateTime dateToDisplay = new DateTime(9999,
13, 1, 17, 4, 32);
// converting DateTime to specified string
string val = dateToDisplay.ToString(format);
// display the converted ulong value
Console.WriteLine(" {0} ", val);
}
}
Converts the value of the currentDateTime object to its equivalent string
The date and time are outside the range of dates supported by the calendar used by the current culture.
Exception Thrown:System.ArgumentOutOfRangeException
參考:
相關用法
- C# Math.Max()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTime.ToString() Method in C# | Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。