DateTimeOffset.ToString 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다.
오버로드
ToString() |
현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다. |
ToString(IFormatProvider) |
지정된 문화권별 형식 정보를 사용하여 현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다. |
ToString(String) |
지정된 형식을 사용하여 현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다. |
ToString(String, IFormatProvider) |
지정된 형식 및 문화권별 형식 정보를 사용하여 현재 DateTimeOffset 개체의 값을 해당 문자열 표현으로 변환합니다. |
ToString()
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다.
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
반환
문자열 끝에 추가되는 오프셋을 포함하는 DateTimeOffset 개체의 문자열 표현입니다.
예외
날짜 및 시간이 현재 문화권에서 사용되는 달력에서 지원하는 날짜 범위를 벗어납니다.
예제
다음 예제에서는 메서드에 대한 호출을 ToString() 보여 줍니다. 현재 문화권이 en-us인 시스템에 해당 출력을 표시합니다.
DateTimeOffset thisDate;
// Show output for UTC time
thisDate = DateTimeOffset.UtcNow;
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 7:13:50 PM +00:00
// Show output for local time
thisDate = DateTimeOffset.Now;
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 12:13:50 PM -07:00
// Show output for arbitrary time offset
thisDate = thisDate.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine(thisDate.ToString()); // Displays 3/28/2007 2:13:50 PM -05:00
// Show output for UTC time
let thisDate = DateTimeOffset.UtcNow
printfn $"{thisDate.ToString()}" // Displays 3/28/2007 7:13:50 PM +00:00
// Show output for local time
let thisDate = DateTimeOffset.Now
printfn $"{thisDate.ToString()}" // Displays 3/28/2007 12:13:50 PM -07:00
// Show output for arbitrary time offset
let thisDate = thisDate.ToOffset(TimeSpan(-5, 0, 0))
printfn $"{thisDate.ToString()}" // Displays 3/28/2007 2:13:50 PM -05:00
Dim thisDate As DateTimeOffset
' Show output for UTC time
thisDate = DateTimeOffset.UtcNow
Console.WriteLine(thisDate.ToString()) ' Displays 3/28/2007 7:13:50 PM +00:00
' Show output for local time
thisDate = DateTimeOffset.Now
Console.WriteLine(thisDate.ToString()) ' Displays 3/28/2007 12:13:50 PM -07:00
' Show output for arbitrary time offset
thisDate = thisDate.ToOffset(new TimeSpan(-5, 0, 0))
Console.WriteLine(thisDate.ToString()) ' Displays 3/28/2007 2:13:50 PM -05:00
설명
이 메서드의 반환 값은 문자열 끝에 DateTime.ToString() 추가된 오프셋 뒤에 공백이 포함된다는 점을 제외하고 메서드의 반환 값과 동일합니다. 즉, 각 요소가 이전 요소와 공백으로 구분된 짧은 날짜 패턴, 긴 시간 패턴 및 zzz
사용자 지정 형식 문자열을 사용하여 출력의 서식을 지정합니다. 예를 들어 2008년 1월 12일 오후 ToString() 6:15:50 값을 반환하는 경우 DateTime.ToString() 는 UTC(협정 세계시)에 8시간 뒤인 시간에 대해 1/12/2008 6:15:50 PM -08:00 값을 반환합니다.
이 메서드는 현재 문화권에서 파생된 서식 정보를 사용합니다. 자세한 내용은 CurrentCulture를 참조하세요. 메서드의 ToString 다른 오버로드를 사용하면 서식을 사용할 문화권을 지정하고 값의 DateTimeOffset 출력 패턴을 정의할 수 있습니다.
호출자 참고
메서드는 ToString() 현재 문화권에서 사용하는 달력의 날짜 및 시간의 문자열 표현을 반환합니다. 현재 DateTimeOffset 인스턴스의 값이 보다 MinSupportedDateTime 이전 또는 이후 MaxSupportedDateTime인 경우 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다. 다음 예제에서 이에 대해 설명합니다. 현재 문화권이 아랍어(시리아)인 경우 클래스 범위를 벗어난 날짜의 HijriCalendar 형식을 지정하려고 시도합니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTimeOffset date1 = new DateTimeOffset(new DateTime(550, 1, 1),
TimeSpan.Zero);
CultureInfo dft;
CultureInfo arSY = new CultureInfo("ar-SY");
arSY.DateTimeFormat.Calendar = new HijriCalendar();
// Change current culture to ar-SY.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = arSY;
// Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString());
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
}
// Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
// The example displays the following output:
// 01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999
open System
open System.Globalization
open System.Threading
let date1 = DateTimeOffset(DateTime(550, 1, 1), TimeSpan.Zero)
let arSY = CultureInfo "ar-SY"
arSY.DateTimeFormat.Calendar <- HijriCalendar()
// Change current culture to ar-SY.
let dft = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture <- arSY
// Display the date using the current culture's calendar.
try
printfn $"{date1}"
with :? ArgumentOutOfRangeException ->
printfn $"""{date1.ToString("d", CultureInfo.InvariantCulture)} is earlier than {arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)} or later than {arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)}"""
// Restore the default culture.
Thread.CurrentThread.CurrentCulture <- dft
// The example displays the following output:
// 01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Dim date1 As New DateTimeOffset(#1/1/550#, TimeSpan.Zero)
Dim dft As CultureInfo
Dim arSY As New CultureInfo("ar-SY")
arSY.DateTimeFormat.Calendar = New HijriCalendar()
' Change current culture to ar-SY.
dft = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = arSY
' Display the date using the current culture's calendar.
Try
Console.WriteLine(date1.ToString())
Catch e As ArgumentOutOfRangeException
Console.WriteLine("{0} is earlier than {1:d} or later than {2:d}", _
date1.ToString("d", CultureInfo.InvariantCulture), _
arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture), _
arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture))
End Try
' Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft
End Sub
End Module
' The example displays the following output:
' 01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999
적용 대상
ToString(IFormatProvider)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
지정된 문화권별 형식 정보를 사용하여 현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다.
public:
System::String ^ ToString(IFormatProvider ^ formatProvider);
public string ToString (IFormatProvider formatProvider);
public string ToString (IFormatProvider? formatProvider);
override this.ToString : IFormatProvider -> string
Public Function ToString (formatProvider As IFormatProvider) As String
매개 변수
- formatProvider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.
반환
현재 DateTimeOffset 개체의 값을 formatProvider
에 지정된 대로 나타낸 문자열 표현입니다.
예외
날짜 및 시간이 formatProvider
에서 사용되는 달력에서 지원하는 날짜 범위를 벗어납니다.
예제
다음 예제에서는 DateTimeOffset 고정 문화권을 나타내는 개체와 4개의 다른 문화권을 사용하는 CultureInfo 개체를 표시합니다.
CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture,
new CultureInfo("en-us"),
new CultureInfo("fr-fr"),
new CultureInfo("de-DE"),
new CultureInfo("es-ES")};
DateTimeOffset thisDate = new DateTimeOffset(2007, 5, 1, 9, 0, 0,
TimeSpan.Zero);
foreach (CultureInfo culture in cultures)
{
string cultureName;
if (string.IsNullOrEmpty(culture.Name))
cultureName = culture.NativeName;
else
cultureName = culture.Name;
Console.WriteLine("In {0}, {1}",
cultureName, thisDate.ToString(culture));
}
// The example produces the following output:
// In Invariant Language (Invariant Country), 05/01/2007 09:00:00 +00:00
// In en-US, 5/1/2007 9:00:00 AM +00:00
// In fr-FR, 01/05/2007 09:00:00 +00:00
// In de-DE, 01.05.2007 09:00:00 +00:00
// In es-ES, 01/05/2007 9:00:00 +00:00
let cultures =
[| CultureInfo.InvariantCulture
CultureInfo "en-us"
CultureInfo "fr-fr"
CultureInfo "de-DE"
CultureInfo "es-ES" |]
let thisDate = DateTimeOffset(2007, 5, 1, 9, 0, 0, TimeSpan.Zero)
for culture in cultures do
let cultureName =
if String.IsNullOrEmpty culture.Name then
culture.NativeName
else
culture.Name
printfn $"In {cultureName}, {thisDate.ToString culture}"
// The example produces the following output:
// In Invariant Language (Invariant Country), 05/01/2007 09:00:00 +00:00
// In en-US, 5/1/2007 9:00:00 AM +00:00
// In fr-FR, 01/05/2007 09:00:00 +00:00
// In de-DE, 01.05.2007 09:00:00 +00:00
// In es-ES, 01/05/2007 9:00:00 +00:00
Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture, _
New CultureInfo("en-us"), _
New CultureInfo("fr-fr"), _
New CultureInfo("de-DE"), _
New CultureInfo("es-ES")}
Dim thisDate As New DateTimeOffset(#5/1/2007 9:00AM#, TimeSpan.Zero)
For Each culture As CultureInfo In cultures
Dim cultureName As String
If String.IsNullOrEmpty(culture.Name) Then
cultureName = culture.NativeName
Else
cultureName = culture.Name
End If
Console.WriteLine("In {0}, {1}", _
cultureName, thisDate.ToString(culture))
Next
' The example produces the following output:
' In Invariant Language (Invariant Country), 05/01/2007 09:00:00 +00:00
' In en-US, 5/1/2007 9:00:00 AM +00:00
' In fr-FR, 01/05/2007 09:00:00 +00:00
' In de-DE, 01.05.2007 09:00:00 +00:00
' In es-ES, 01/05/2007 9:00:00 +00:00
설명
이 메서드의 반환 값은 문자열 끝에 추가된 오프셋 뒤에 공백이 포함된다는 점을 제외하고 메서드의 DateTime.ToString 동일한 오버로드와 동일합니다. 즉, 각 요소가 이전 요소와 공백으로 구분된 짧은 날짜 패턴, 긴 시간 패턴 및 zzz
사용자 지정 형식 문자열을 사용하여 출력의 서식을 지정합니다.
이러한 세 요소의 형식은 매개 변수로 formatProvider
정의됩니다. 매개 변수는 formatProvider
다음 중 하나일 수 있습니다.
CultureInfo 반환된 문자열에 서식 규칙이 적용되는 문화권을 나타내는 개체입니다. 속성에서 반환된 CultureInfo.DateTimeFormat 개체는 DateTimeFormatInfo 반환된 문자열의 서식을 정의합니다.
DateTimeFormatInfo 날짜 및 시간 데이터의 형식을 정의하는 개체입니다.
가 이 null
DateTimeFormatInfo 면 formatProvider
현재 문화권과 연결된 개체가 사용됩니다(참조CurrentCulture).
호출자 참고
메서드는 ToString(IFormatProvider) 매개 변수가 나타내는 문화권에서 사용하는 달력의 날짜 및 시간의 문자열 표현을 formatProvider
반환합니다. 해당 달력은 속성에 Calendar 의해 정의됩니다. 현재 DateTimeOffset 인스턴스의 값이 보다 MinSupportedDateTime 이전 또는 이후 MaxSupportedDateTime인 경우 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다. 다음 예제에서 이에 대해 설명합니다. 클래스 범위를 벗어난 날짜의 서식을 지정하려고 시도합니다 JapaneseCalendar .
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo jaJP = new CultureInfo("ja-JP");
jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1867, 1, 1),
TimeSpan.Zero);
try {
Console.WriteLine(date1.ToString(jaJP));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
date1,
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
}
}
// The example displays the following output:
// 1/1/1867 is earlier than 9/8/1868 or later than 12/31/9999
open System
open System.Globalization
let jaJP = CultureInfo "ja-JP"
jaJP.DateTimeFormat.Calendar <- JapaneseCalendar()
let date1 = DateTimeOffset(DateTime(1867, 1, 1), TimeSpan.Zero)
try
printfn $"{date1.ToString jaJP}"
with :? ArgumentOutOfRangeException ->
printfn $"{date1:d} is earlier than {jaJP.DateTimeFormat.Calendar.MinSupportedDateTime:d} or later than {jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime:d}"
// The example displays the following output:
// 1/1/1867 is earlier than 9/8/1868 or later than 12/31/9999
Imports System.Globalization
Module Example
Public Sub Main()
Dim jaJP As New CultureInfo("ja-JP")
jaJP.DateTimeFormat.Calendar = New JapaneseCalendar()
Dim date1 As New DateTimeOffset(#01/01/1867#, TimeSpan.Zero)
Try
Console.WriteLine(date1.ToString(jaJP))
Catch e As ArgumentOutOfRangeException
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}", _
date1, _
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime, _
jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime)
End Try
End Sub
End Module
' The example displays the following output:
' 1/1/1867 is earlier than 9/8/1868 or later than 12/31/9999
적용 대상
ToString(String)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
지정된 형식을 사용하여 현재 DateTimeOffset 개체의 값을 해당하는 문자열 표현으로 변환합니다.
public:
System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String
매개 변수
- format
- String
서식 문자열입니다.
반환
현재 DateTimeOffset 개체의 값을 format
에 지정된 대로 나타낸 문자열 표현입니다.
예외
format
의 길이가 1이고 DateTimeFormatInfo에 대해 정의된 표준 형식 지정자 문자 중 하나가 아닙니다.
또는
format
에 올바른 사용자 지정 형식 패턴이 포함되어 있지 않습니다.
날짜 및 시간이 현재 문화권에서 사용되는 달력에서 지원하는 날짜 범위를 벗어납니다.
예제
다음 예제에서는 DateTimeOffset 각 표준 날짜 및 시간 형식 지정자를 사용 하 여 콘솔에 개체를 표시 합니다. 출력은 en-us 문화권을 사용하여 형식이 지정됩니다.
DateTimeOffset outputDate = new DateTimeOffset(2007, 10, 31, 21, 0, 0,
new TimeSpan(-8, 0, 0));
string specifier;
// Output date using each standard date/time format specifier
specifier = "d";
// Displays d: 10/31/2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "D";
// Displays D: Wednesday, October 31, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "t";
// Displays t: 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "T";
// Displays T: 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "f";
// Displays f: Wednesday, October 31, 2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "F";
// Displays F: Wednesday, October 31, 2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "g";
// Displays g: 10/31/2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "G";
// Displays G: 10/31/2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "M"; // 'm' is identical
// Displays M: October 31
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "R"; // 'r' is identical
// Displays R: Thu, 01 Nov 2007 05:00:00 GMT
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "s";
// Displays s: 2007-10-31T21:00:00
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
specifier = "u";
// Displays u: 2007-11-01 05:00:00Z
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
// Specifier is not supported
specifier = "U";
try
{
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
}
catch (FormatException)
{
Console.WriteLine("{0}: Not supported.", specifier);
}
specifier = "Y"; // 'y' is identical
// Displays Y: October, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
let outputDate = DateTimeOffset(2007, 10, 31, 21, 0, 0, TimeSpan(-8, 0, 0))
// Output date using each standard date/time format specifier
let specifier = "d"
// Displays d: 10/31/2007
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "D"
// Displays D: Wednesday, October 31, 2007
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "t"
// Displays t: 9:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "T"
// Displays T: 9:00:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "f"
// Displays f: Wednesday, October 31, 2007 9:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "F"
// Displays F: Wednesday, October 31, 2007 9:00:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "g"
// Displays g: 10/31/2007 9:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "G"
// Displays G: 10/31/2007 9:00:00 PM
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "M" // 'm' is identical
// Displays M: October 31
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "R" // 'r' is identical
// Displays R: Thu, 01 Nov 2007 05:00:00 GMT
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "s"
// Displays s: 2007-10-31T21:00:00
printfn $"{specifier}: {outputDate.ToString specifier}"
let specifier = "u"
// Displays u: 2007-11-01 05:00:00Z
printfn $"{specifier}: {outputDate.ToString specifier}"
// Specifier is not supported
let specifier = "U"
try
printfn $"{specifier}: {outputDate.ToString specifier}"
with :? FormatException ->
printfn $"{specifier}: Not supported."
let specifier = "Y" // 'y' is identical
// Displays Y: October, 2007
printfn $"{specifier}: {outputDate.ToString specifier}"
Dim outputDate As New DateTimeOffset(#10/31/2007 9:00PM#, _
New TimeSpan(-8, 0, 0))
Dim specifier As String
' Output date using each standard date/time format specifier
specifier = "d"
' Displays d: 10/31/2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "D"
' Displays D: Wednesday, October 31, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "t"
' Displays t: 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "T"
' Displays T: 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "f"
' Displays f: Wednesday, October 31, 2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "F"
' Displays F: Wednesday, October 31, 2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "g"
' Displays g: 10/31/2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "G"
' Displays G: 10/31/2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "M" ' 'm' is identical
' Displays M: October 31
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "R" ' 'r' is identical
' Displays R: Thu, 01 Nov 2007 05:00:00 GMT
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "s"
' Displays s: 2007-10-31T21:00:00
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
specifier = "u"
' Displays u: 2007-11-01 05:00:00Z
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
' Specifier is not supported
specifier = "U"
Try
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
Catch e As FormatException
Console.WriteLine("{0}: Not supported.", specifier)
End Try
specifier = "Y" ' 'y' is identical
' Displays Y: October, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier))
설명
매개 변수에는 format
단일 형식 지정자 문자( 표준 날짜 및 시간 형식 문자열 참조) 또는 반환된 문자열의 형식을 정의하는 사용자 지정 형식 패턴( 사용자 지정 날짜 및 시간 형식 문자열 참조)이 포함되어야 합니다. 가 null이거나 빈 문자열("")DateTimeOffset인 경우 format
값은 기본 형식을 사용하여 출력됩니다.
다음 표에서는 와 함께 사용할 때 특정 형식 지정자의 정확한 작업을 보여 줍니다. 와 함께 DateTimeOffset사용할 때의 동작과 DateTime는 다릅니다.
기존 형식 지정자 | 새 동작 |
---|---|
"K" | 날짜와 시간을 왕복하도록 설계되었습니다. 를 사용하면 DateTimeOffset"zzz"에 매핑됩니다(오프셋은 항상 시간 및 분으로 표시됨). "K"는 사용자 지정 형식 지정자입니다. 에서 format 단일 문자로 표시할 수 없습니다. |
"U" | 지원되지 않습니다. |
"r" | 개체를 DateTimeOffset UTC(협정 세계시)로 변환하고 사용자 지정 형식 문자열 ddd, dd MMM yyyy HH:mm:ss GMT 을 사용하여 출력합니다. |
"u" | 개체를 DateTimeOffset UTC로 변환하고 형식 yyyy-MM-dd HH:mm:ssZ 을 사용하여 출력합니다. |
나머지 표준 날짜 및 시간 형식 지정자는 메서드와 마찬가지로 메서드와 ToString(String) 동일하게 동작합니다 ToString .
이 메서드는 현재 문화권에서 파생된 서식 정보를 사용합니다. 자세한 내용은 CurrentCulture를 참조하세요.
호출자 참고
메서드는 ToString(String) 현재 문화권에서 사용하는 달력의 날짜 및 시간의 문자열 표현을 반환합니다. 현재 DateTimeOffset 인스턴스의 값이 보다 MinSupportedDateTime 이전 또는 이후 MaxSupportedDateTime인 경우 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다. 다음 예제에서 이에 대해 설명합니다. 현재 문화권이 히브리어(이스라엘)인 경우 클래스 범위를 벗어난 날짜의 HebrewCalendar 형식을 지정하려고 합니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1550, 7, 21),
TimeSpan.Zero);
CultureInfo dft;
CultureInfo heIL = new CultureInfo("he-IL");
heIL.DateTimeFormat.Calendar = new HebrewCalendar();
// Change current culture to he-IL.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = heIL;
// Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString("G"));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
}
// Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
// The example displays the following output:
// 07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
open System
open System.Globalization
open System.Threading
let date1 = DateTimeOffset(DateTime(1550, 7, 21), TimeSpan.Zero)
let heIL = CultureInfo "he-IL"
heIL.DateTimeFormat.Calendar <- HebrewCalendar()
// Change current culture to he-IL.
let dft = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture <- heIL
// Display the date using the current culture's calendar.
try
printfn $"{date1:G}"
with :? ArgumentOutOfRangeException ->
printfn $"""{date1.ToString("d", CultureInfo.InvariantCulture)} is earlier than {heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)} or later than {heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)}"""
// Restore the default culture.
Thread.CurrentThread.CurrentCulture <- dft
// The example displays the following output:
// 07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Dim date1 As New DateTimeOffset(#7/21/1550#, TimeSpan.Zero)
Dim dft As CultureInfo
Dim heIL As New CultureInfo("he-IL")
heIL.DateTimeFormat.Calendar = New HebrewCalendar()
' Change current culture to he-IL.
dft = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = heIL
' Display the date using the current culture's calendar.
Try
Console.WriteLine(date1.ToString("G"))
Catch e As ArgumentOutOfRangeException
Console.WriteLine("{0} is earlier than {1} or later than {2}", _
date1.ToString("d", CultureInfo.InvariantCulture), _
heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture), _
heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture))
End Try
' Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft
End Sub
End Module
' The example displays the following output:
' 07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
추가 정보
적용 대상
ToString(String, IFormatProvider)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
지정된 형식 및 문화권별 형식 정보를 사용하여 현재 DateTimeOffset 개체의 값을 해당 문자열 표현으로 변환합니다.
public:
virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ formatProvider);
public string ToString (string format, IFormatProvider formatProvider);
public string ToString (string? format, IFormatProvider? formatProvider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, formatProvider As IFormatProvider) As String
매개 변수
- format
- String
서식 문자열입니다.
- formatProvider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.
반환
현재 DateTimeOffset 개체의 값을 format
및 provider
에 지정된 대로 나타낸 문자열 표현입니다.
구현
예외
format
의 길이가 1이고 DateTimeFormatInfo에 대해 정의된 표준 형식 지정자 문자 중 하나가 아닙니다.
또는
format
에 올바른 사용자 지정 형식 패턴이 포함되어 있지 않습니다.
날짜 및 시간이 formatProvider
에서 사용되는 달력에서 지원하는 날짜 범위를 벗어납니다.
예제
다음 예제에서는 메서드를 ToString(String, IFormatProvider) 사용 하 여 여러 다른 문화권에 DateTimeOffset 대 한 사용자 지정 형식 문자열을 사용 하 여 개체를 표시 합니다.
DateTimeOffset outputDate = new DateTimeOffset(2007, 11, 1, 9, 0, 0,
new TimeSpan(-7, 0, 0));
string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
// Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("fr-FR")));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("es-ES")));
// The example displays the following output to the console:
// Thursday, Nov 01 2007 09:00:00 -07:00
// Thursday, Nov 01 2007 09:00:00 -07:00
// jeudi, nov. 01 2007 09:00:00 -07:00
// jueves, nov 01 2007 09:00:00 -07:00
let outputDate = DateTimeOffset(2007, 11, 1, 9, 0, 0, TimeSpan(-7, 0, 0))
let format = "dddd, MMM dd yyyy HH:mm:ss zzz"
// Output date and time using custom format specification
printfn $"{outputDate.ToString(format, null)}"
printfn $"{outputDate.ToString(format, CultureInfo.InvariantCulture)}"
printfn $"""{outputDate.ToString(format, CultureInfo "fr-FR")}"""
printfn $"""{outputDate.ToString(format, CultureInfo "es-ES")}"""
// The example displays the following output to the console:
// Thursday, Nov 01 2007 09:00:00 -07:00
// Thursday, Nov 01 2007 09:00:00 -07:00
// jeudi, nov. 01 2007 09:00:00 -07:00
// jueves, nov 01 2007 09:00:00 -07:00
Dim outputDate As New DateTimeOffset(#11/1/2007 9:00AM#, _
New TimeSpan(-7, 0, 0))
Dim format As String = "dddd, MMM dd yyyy HH:mm:ss zzz"
' Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, Nothing))
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture))
Console.WriteLine(outputDate.ToString(format, _
New CultureInfo("fr-FR")))
Console.WriteLine(outputDate.ToString(format, _
New CultureInfo("es-ES")))
' The example displays the following output to the console:
' Thursday, Nov 01 2007 09:00:00 -07:00
' Thursday, Nov 01 2007 09:00:00 -07:00
' jeudi, nov. 01 2007 09:00:00 -07:00
' jueves, nov 01 2007 09:00:00 -07:00
설명
매개 변수에는 format
단일 형식 지정자 문자( 표준 날짜 및 시간 형식 문자열 참조) 또는 사용자 지정 형식 패턴( 사용자 지정 날짜 및 시간 형식 문자열 참조)이 포함되어야 합니다. 가 null이거나 빈 문자열("")DateTimeOffset인 경우 format
개체는 기본 형식을 사용하여 출력됩니다.
다음 표에서는 와 함께 사용할 때 특정 형식 지정자의 정확한 작업을 보여 줍니다. 와 함께 DateTimeOffset사용할 때의 동작과 DateTime는 다릅니다.
기존 형식 지정자 | 새 동작 |
---|---|
"K" | 날짜와 시간을 왕복하도록 설계되었습니다. 를 사용하면 DateTimeOffset"zzz"에 매핑됩니다(오프셋은 항상 시간 및 분으로 표시됨). "K"는 사용자 지정 형식 지정자입니다. 에서 format 단일 문자로 표시할 수 없습니다. |
"U" | 지원되지 않습니다. |
"r" | 개체를 DateTimeOffset UTC(협정 세계시)로 변환하고 사용자 지정 형식 문자열 ddd, dd MMM yyyy HH:mm:ss GMT 을 사용하여 출력합니다. |
"u" | DateTimeOffset 값을 UTC로 변환하고 형식yyyy-MM-dd HH:mm:ssZ 을 사용하여 출력합니다. |
나머지 표준 날짜 및 시간 형식 지정자는 메서드와 마찬가지로 메서드와 ToString(String) 동일하게 동작합니다 ToString .
표준 형식 지정자와 날짜 및 시간 구성 요소의 기호 및 이름에 해당하는 패턴은 매개 변수에 formatProvider
의해 정의됩니다. 매개 변수는 formatProvider
다음 중 하나일 수 있습니다.
CultureInfo 에서 서식을 사용하는
input
문화권을 나타내는 개체입니다. 속성에서 반환된 CultureInfo.DateTimeFormat 개체는 DateTimeFormatInfo 에서input
사용되는 서식을 정의합니다.DateTimeFormatInfo 날짜 및 시간 데이터의 형식을 정의하는 개체입니다.
가 이 null
DateTimeFormatInfo 면 formatProvider
현재 문화권과 연결된 개체가 사용됩니다(참조CurrentCulture).
호출자 참고
메서드는 ToString(String, IFormatProvider) 매개 변수에서 사용하는 달력의 날짜 및 시간의 문자열 표현을 formatProvider
반환합니다. 해당 달력은 속성에 Calendar 의해 정의됩니다. 현재 DateTimeOffset 인스턴스의 값이 보다 MinSupportedDateTime 이전 또는 이후 MaxSupportedDateTime인 경우 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다. 다음 예제에서 이에 대해 설명합니다. 클래스 범위를 벗어난 날짜의 서식을 지정하려고 시도합니다 UmAlQuraCalendar .
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo arSA = new CultureInfo("ar-SA");
arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar();
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1890, 9, 10),
TimeSpan.Zero);
try {
Console.WriteLine(date1.ToString("d", arSA));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
date1,
arSA.DateTimeFormat.Calendar.MinSupportedDateTime,
arSA.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
}
}
// The example displays the following output:
// 9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029
open System
open System.Globalization
let arSA = CultureInfo "ar-SA"
arSA.DateTimeFormat.Calendar <- UmAlQuraCalendar()
let date1 = DateTimeOffset(DateTime(1890, 9, 10), TimeSpan.Zero)
try
printfn $"""{date1.ToString("d", arSA)}"""
with :? ArgumentOutOfRangeException ->
printfn $"{date1:d} is earlier than {arSA.DateTimeFormat.Calendar.MinSupportedDateTime:d} or later than {arSA.DateTimeFormat.Calendar.MaxSupportedDateTime:d}"
// The example displays the following output:
// 9/10/1890 is earlier than 4/30/1900 or later than 11/16/2077
Imports System.Globalization
Module Example
Public Sub Main()
Dim arSA As New CultureInfo("ar-SA")
arSA.DateTimeFormat.Calendar = New UmAlQuraCalendar()
Dim date1 As New DateTimeOffset(#09/10/1890#, TimeSpan.Zero)
Try
Console.WriteLine(date1.ToString("d", arSA))
Catch e As ArgumentOutOfRangeException
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}", _
date1, _
arSA.DateTimeFormat.Calendar.MinSupportedDateTime, _
arSA.DateTimeFormat.Calendar.MaxSupportedDateTime)
End Try
End Sub
End Module
' The example displays the following output:
' 9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029
추가 정보
- 표준 날짜 및 시간 형식 문자열
- 사용자 지정 날짜 및 시간 형식 문자열
- 샘플: .NET Core WinForms 서식 유틸리티(C#)
- 샘플: .NET Core WinForms 서식 유틸리티(Visual Basic)
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기