次の方法で共有


DateTime.ToString メソッド ()

このインスタンスの値をそれと等価な文字列に変換します。

Overrides Overloads Public Function ToString() As String
[C#]
public override string ToString();
[C++]
public: String* ToString();
[JScript]
public override function ToString() : String;

戻り値

このインスタンスの値の文字列形式。

解説

このインスタンスの値は、 書式設定の概要 のトピックで説明しているように、一般書式指定子 "G" を使用して書式指定されます。戻り値は、 ToString が返す値 ("G"、null) と同じです。

このメソッドは、現在のカルチャから派生した形式指定情報を使用します。詳細については、「 CurrentCulture 」を参照してください。

使用例

次のサンプルでは、Invariant DateTimeFormatInfo を使用して DateTime 値を書式指定するさまざまな方法を示します。

 
Option Explicit
Option Strict

Imports System
Imports System.Globalization

Public Class MainClass
    
    Public Shared Sub Main()
        Dim dt As DateTime = DateTime.Now
        Dim myformat() As 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"}
        Dim mydate As String
        Dim i As Integer
        For i = 0 To myformat.Length - 1
            mydate = dt.ToString(myformat(i), DateTimeFormatInfo.InvariantInfo)
            Console.WriteLine(String.Concat(myformat(i), " :", mydate))
        Next i

    ' 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
    End Sub 'Main 
End Class 'MainClass

[C#] 
using System;
using System.Globalization; 

public class MainClass { 
   public static void Main(string[] args)  {
       DateTime dt = DateTime.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.Length; i++) {
           date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
           Console.WriteLine(String.Concat(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
   */
   }
}

[C++] 
#using <mscorlib.dll>
 
 using namespace System;
 using namespace System::Globalization; 
 
 void main ()
 {
   DateTime dt = DateTime::Now;
   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
    */
 }

[JScript] 
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 NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

DateTime 構造体 | DateTime メンバ | System 名前空間 | DateTime.ToString オーバーロードの一覧 | DateTimeFormatInfo | CultureInfo