当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# DateTime.ToString()方法用法及代码示例


此方法用于将当前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

参考:




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DateTime.ToString() Method in C# | Set – 1。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。