DateTime.ToString メソッド
インスタンスの値をそれと等価な文字列形式に変換します。
オーバーロードの一覧
このインスタンスの値をそれと等価な文字列に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Overrides Public Function ToString() As String
指定したカルチャ固有の書式情報を使用して、このインスタンスの値をそれと等価な文字列形式に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String
[JScript] public function ToString(IFormatProvider) : String;
指定した書式を使用して、このインスタンスの値をそれと等価な文字列形式に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function ToString(String) As String
指定した書式およびカルチャ固有の書式情報を使用して、このインスタンスの値をそれと等価の文字列形式に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Overridable Function ToString(String, IFormatProvider) As String
[C#] public virtual string ToString(string, IFormatProvider);
[C++] public: virtual String* ToString(String*, IFormatProvider*);
[JScript] public function ToString(String, IFormatProvider) : String;
使用例
次のサンプルでは、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
*/