TimeSpan.ToString 메서드

정의

현재 TimeSpan 개체의 값을 해당하는 문자열 표현으로 변환합니다.

오버로드

ToString(String, IFormatProvider)

지정된 형식과 문화권별 형식 정보를 사용하여 현재 TimeSpan 개체의 값을 해당 문자열 표현으로 변환합니다.

ToString(String)

지정된 형식을 사용하여 현재 TimeSpan 개체의 값을 해당하는 문자열 표현으로 변환합니다.

ToString()

현재 TimeSpan 개체의 값을 해당하는 문자열 표현으로 변환합니다.

ToString(String, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식과 문화권별 형식 정보를 사용하여 현재 TimeSpan 개체의 값을 해당 문자열 표현으로 변환합니다.

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

표준 또는 사용자 지정 TimeSpan 형식 문자열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

반환

formatformatProvider에서 지정한 현재 TimeSpan 값에 대한 문자열 표현입니다.

구현

예외

format 매개 변수가 인식되거나 지원되지 않는 경우.

예제

다음 예제에서는 메서드를 ToString(String, IFormatProvider) 호출하여 두 시간 간격의 형식을 지정합니다. 이 예제에서는 각 형식 문자열에 대해 메서드를 두 번 호출하고, 먼저 en-US 문화권의 규칙을 사용하여 표시한 다음 fr-FR 문화권의 규칙을 사용하여 표시합니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan[] intervals = { new TimeSpan(38, 30, 15), 
                               new TimeSpan(16, 14, 30) }; 
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("fr-FR") };
      string[] formats = {"c", "g", "G", @"hh\:mm\:ss" };
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}\n", 
                        "Interval", cultures[0].Name, cultures[1].Name);

      foreach (var interval in intervals) {
         foreach (var fmt in formats)
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures[0]), 
                              interval.ToString(fmt, cultures[1]));
         Console.WriteLine();
      }  
   }
}
// The example displays the following output:
//        Interval      Format                   en-US                   fr-FR
//    
//      1.14:30:15           c              1.14:30:15              1.14:30:15
//      1.14:30:15           g              1:14:30:15              1:14:30:15
//      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
//      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
//    
//        16:14:30           c                16:14:30                16:14:30
//        16:14:30           g                16:14:30                16:14:30
//        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
//        16:14:30  hh\:mm\:ss                16:14:30                16:14:30
open System
open System.Globalization

let intervals = 
    [| TimeSpan(38, 30, 15)
       TimeSpan(16, 14, 30) |] 
let cultures = 
    [| CultureInfo "en-US"
       CultureInfo "fr-FR" |]
let formats = [| "c"; "g"; "G"; @"hh\:mm\:ss" |]
printfn $"""{"Interval",12}      Format  {cultures[0].Name,22}  {cultures[1].Name,22}\n""" 

for interval in intervals do
    for fmt in formats do
        printfn $"{interval,12}  {fmt,10}  {interval.ToString(fmt, cultures[0]),22}  {interval.ToString(fmt, cultures[1]),22}"
    printfn ""
// The example displays the following output:
//        Interval      Format                   en-US                   fr-FR
//    
//      1.14:30:15           c              1.14:30:15              1.14:30:15
//      1.14:30:15           g              1:14:30:15              1:14:30:15
//      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
//      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
//    
//        16:14:30           c                16:14:30                16:14:30
//        16:14:30           g                16:14:30                16:14:30
//        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
//        16:14:30  hh\:mm\:ss                16:14:30                16:14:30
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervals() As TimeSpan = { New TimeSpan(38, 30, 15), 
                                      New TimeSpan(16, 14, 30) } 
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("fr-FR") }
      Dim formats() As String = {"c", "g", "G", "hh\:mm\:ss" }
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}", 
                        "Interval", cultures(0).Name, cultures(1).Name)
      Console.WriteLine()
      For Each interval In intervals
         For Each fmt In formats
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures(0)), 
                              interval.ToString(fmt, cultures(1)))
         Next
         Console.WriteLine()
      Next                                                                                                                                            
   End Sub
End Module
' The example displays the following output:
'        Interval      Format                   en-US                   fr-FR
'    
'      1.14:30:15           c              1.14:30:15              1.14:30:15
'      1.14:30:15           g              1:14:30:15              1:14:30:15
'      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
'      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15
'    
'        16:14:30           c                16:14:30                16:14:30
'        16:14:30           g                16:14:30                16:14:30
'        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
'        16:14:30  hh\:mm\:ss                16:14:30                16:14:30

설명

매개 변수는 format 값에 TimeSpan 유효한 표준 또는 사용자 지정 형식 지정자일 수 있습니다. 가 String.Empty 또는 이nullformat 현재 TimeSpan 개체의 반환 값은 공통 형식 지정자("c")로 서식이 지정됩니다. format이 다른 값이면 메서드는 을 FormatExceptionthrow합니다.

중요

값에 대한 TimeSpan 사용자 지정 형식 문자열에는 날짜 또는 시간 구분 기호가 포함되지 않습니다. 이러한 요소를 형식 문자열에 포함하려면 해당 요소를 문자 리터럴로 처리해야 합니다. 자세한 내용은 예제를 참조하고 사용자 지정 TimeSpan 형식 문자열 항목을 참조하세요.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

formatProvider 매개 변수는 반환된 IFormatProvider 문자열의 형식에 대한 문화권별 정보를 제공하는 구현입니다. 매개 변수는 formatProvider 다음 중 어느 것일 수 있습니다.

가 이 nullDateTimeFormatInfoformatProvider 현재 문화권과 연결된 개체가 사용됩니다. 가 사용자 지정 형식 문자열인 formatProvider 경우 format 매개 변수는 무시됩니다.

추가 정보

적용 대상

ToString(String)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식을 사용하여 현재 TimeSpan 개체의 값을 해당하는 문자열 표현으로 변환합니다.

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

표준 또는 사용자 지정 TimeSpan 형식 문자열입니다.

반환

format 매개 변수로 지정된 서식에 따른 현재 TimeSpan 값의 문자열 표현입니다.

예외

format 매개 변수가 인식되거나 지원되지 않는 경우.

예제

다음 예제에서는 표준 및 사용자 지정 TimeSpan 형식 문자열을 사용하여 값 배열 TimeSpan 에서 각 요소의 문자열 표현을 표시합니다.

TimeSpan[] spans = { 
   TimeSpan.Zero, 
   new TimeSpan(-14, 0, 0, 0, 0), 
   new TimeSpan(1, 2, 3), 
   new TimeSpan(0, 0, 0, 0, 250), 
   new TimeSpan(99, 23, 59, 59, 999),
   new TimeSpan(3, 0, 0), 
   new TimeSpan(0, 0, 0, 0, 25) 
};

string[] fmts = { "c", "g", "G", @"hh\:mm\:ss", "%m' min.'" };
foreach (TimeSpan span in spans)
{
   foreach (string fmt in fmts)
      Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt));

   Console.WriteLine();
}
// The example displays the following output:
//       c: 00:00:00
//       g: 0:00:00
//       G: 0:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: -14.00:00:00
//       g: -14:0:00:00
//       G: -14:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 01:02:03
//       g: 1:02:03
//       G: 0:01:02:03.0000000
//       hh\:mm\:ss: 01:02:03
//       %m' min.': 2 min.
//       
//       c: 00:00:00.2500000
//       g: 0:00:00.25
//       G: 0:00:00:00.2500000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 99.23:59:59.9990000
//       g: 99:23:59:59.999
//       G: 99:23:59:59.9990000
//       hh\:mm\:ss: 23:59:59
//       %m' min.': 59 min.
//       
//       c: 03:00:00
//       g: 3:00:00
//       G: 0:03:00:00.0000000
//       hh\:mm\:ss: 03:00:00
//       %m' min.': 0 min.
//       
//       c: 00:00:00.0250000
//       g: 0:00:00.025
//       G: 0:00:00:00.0250000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
let spans = 
    [| TimeSpan.Zero
       TimeSpan(-14, 0, 0, 0, 0)
       TimeSpan(1, 2, 3)
       TimeSpan(0, 0, 0, 0, 250)
       TimeSpan(99, 23, 59, 59, 999)
       TimeSpan(3, 0, 0)
       TimeSpan(0, 0, 0, 0, 25) |]


let fmts = [| "c"; "g"; "G"; @"hh\:mm\:ss"; "%m' min.'" |]
for span in spans do
    for fmt in fmts do
        printfn $"{fmt}: {span.ToString fmt}"
    printfn ""
// The example displays the following output:
//       c: 00:00:00
//       g: 0:00:00
//       G: 0:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: -14.00:00:00
//       g: -14:0:00:00
//       G: -14:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 01:02:03
//       g: 1:02:03
//       G: 0:01:02:03.0000000
//       hh\:mm\:ss: 01:02:03
//       %m' min.': 2 min.
//       
//       c: 00:00:00.2500000
//       g: 0:00:00.25
//       G: 0:00:00:00.2500000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 99.23:59:59.9990000
//       g: 99:23:59:59.999
//       G: 99:23:59:59.9990000
//       hh\:mm\:ss: 23:59:59
//       %m' min.': 59 min.
//       
//       c: 03:00:00
//       g: 3:00:00
//       G: 0:03:00:00.0000000
//       hh\:mm\:ss: 03:00:00
//       %m' min.': 0 min.
//       
//       c: 00:00:00.0250000
//       g: 0:00:00.025
//       G: 0:00:00:00.0250000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
Module Example
   Public Sub Main()
      Dim spans() As TimeSpan = { TimeSpan.Zero, New TimeSpan(-14, 0, 0, 0, 0), 
                                  New TimeSpan(1, 2, 3), 
                                  New TimeSpan(0, 0, 0, 0, 250), 
                                  New TimeSpan(99, 23, 59, 59, 999),
                                  New TimeSpan(3, 0, 0), 
                                  New TimeSpan(0, 0, 0, 0, 25) }
      Dim fmts() As String = { "c", "g", "G", "hh\:mm\:ss", "%m' min.'" }
      For Each span As TimeSpan In spans
         For Each fmt As String In fmts
            Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt))
         Next
         Console.WriteLine()         
      Next
   End Sub
End Module
' The example displays the following output:
'       c: 00:00:00
'       g: 0:00:00
'       G: 0:00:00:00.0000000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: -14.00:00:00
'       g: -14:0:00:00
'       G: -14:00:00:00.0000000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: 01:02:03
'       g: 1:02:03
'       G: 0:01:02:03.0000000
'       hh\:mm\:ss: 01:02:03
'       %m' min.': 2 min.
'       
'       c: 00:00:00.2500000
'       g: 0:00:00.25
'       G: 0:00:00:00.2500000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: 99.23:59:59.9990000
'       g: 99:23:59:59.999
'       G: 99:23:59:59.9990000
'       hh\:mm\:ss: 23:59:59
'       %m' min.': 59 min.
'       
'       c: 03:00:00
'       g: 3:00:00
'       G: 0:03:00:00.0000000
'       hh\:mm\:ss: 03:00:00
'       %m' min.': 0 min.
'       
'       c: 00:00:00.0250000
'       g: 0:00:00.025
'       G: 0:00:00:00.0250000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.

설명

매개 변수는 format 값에 TimeSpan 유효한 표준 또는 사용자 지정 형식 지정자일 수 있습니다. 가 String.Empty 또는 이nullformat 현재 TimeSpan 개체의 반환 값은 공통 형식 지정자("c")로 서식이 지정됩니다. 가 다른 값이면 format 메서드는 을 FormatExceptionthrow합니다.

가 표준 형식 문자열인 경우 format 반환된 문자열의 형식은 현재 문화권의 서식 규칙에 의해 정의됩니다.

중요

값에 대한 TimeSpan 사용자 지정 형식 문자열에는 날짜 또는 시간 구분 기호가 포함되지 않습니다. 이러한 요소를 형식 문자열에 포함하려면 해당 요소를 문자 리터럴로 처리해야 합니다. 자세한 내용은 예제를 참조하고 사용자 지정 TimeSpan 형식 문자열 항목을 참조하세요.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

추가 정보

적용 대상

ToString()

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

현재 TimeSpan 개체의 값을 해당하는 문자열 표현으로 변환합니다.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

반환

현재 TimeSpan 값의 문자열 표현입니다.

예제

다음 예제에서는 여러 TimeSpan 값으로 메서드를 ToString 호출하여 반환되는 문자열을 표시합니다. 예제에서는 메서드를 직접 호출 ToString 하지 않지만 값을 문자열 표현으로 Console.WriteLine 변환하려고 할 때 메서드에서 TimeSpan 호출됩니다.

 TimeSpan span;
 
 // Initialize a time span to zero.
 span = TimeSpan.Zero;
 Console.WriteLine(span);

 // Initialize a time span to 14 days.
 span = new TimeSpan(-14, 0, 0, 0, 0);
 Console.WriteLine(span);

 // Initialize a time span to 1:02:03.
 span = new TimeSpan(1, 2, 3);
 Console.WriteLine(span);

 // Initialize a time span to 250 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 250);
 Console.WriteLine(span);
 
 // Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
 span = new TimeSpan(99, 23, 59, 59, 999);
 Console.WriteLine(span);
 
 // Initialize a time span to 3 hours.
 span = new TimeSpan(3, 0, 0);
 Console.WriteLine(span);
 
 // Initialize a timespan to 25 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 25);
 Console.WriteLine(span);

 // The example displays the following output:
 //       00:00:00
 //       -14.00:00:00
 //       01:02:03
 //       00:00:00.2500000
 //       99.23:59:59.9990000
 //       03:00:00
 //       00:00:00.0250000
// Initialize a time span to zero.
let span = TimeSpan.Zero
printfn $"{span}"

// Initialize a time span to 14 days.
let span = TimeSpan(-14, 0, 0, 0, 0)
printfn $"{span}"

// Initialize a time span to 1:02:03.
let span = TimeSpan(1, 2, 3)
printfn $"{span}"

// Initialize a time span to 250 milliseconds.
let span = TimeSpan(0, 0, 0, 0, 250)
printfn $"{span}"

// Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
let span = TimeSpan(99, 23, 59, 59, 999)
printfn $"{span}"

// Initialize a time span to 3 hours.
let span = TimeSpan(3, 0, 0)
printfn $"{span}"

// Initialize a timespan to 25 milliseconds.
let span = TimeSpan(0, 0, 0, 0, 25)
printfn $"{span}"

// The example displays the following output:
//       00:00:00
//       -14.00:00:00
//       01:02:03
//       00:00:00.2500000
//       99.23:59:59.9990000
//       03:00:00
//       00:00:00.0250000
Module ToString
   Public Sub Main()
      Dim span As TimeSpan
      
      ' Initialize a time span to zero.
      span = TimeSpan.Zero
      Console.WriteLine(span)
      
      ' Initialize a time span to 14 days.
      span = New TimeSpan(-14, 0, 0, 0, 0)
      Console.WriteLine(span)
     
      ' Initialize a time span to 1:02:03.
      span = New TimeSpan(1, 2, 3)
      Console.WriteLine(span)
      
      
      ' Initialize a time span to 250 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 250)
      Console.WriteLine(span)
      
      ' Initialize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds.
      span = New TimeSpan(99, 23, 59, 59, 999)
      Console.WriteLine(span)
      
      ' Initialize a time span to 3 hours.
      span = New TimeSpan(3, 0, 0)
      Console.WriteLine(span)
      
      ' Initialize a timespan to 25 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 25)
      Console.WriteLine(span)
   End Sub
End Module
' The example displays the following output:
'       00:00:00
'       -14.00:00:00
'       01:02:03
'       00:00:00.2500000
'       99.23:59:59.9990000
'       03:00:00
'       00:00:00.0250000

설명

반환된 문자열은 "c" 형식 지정자를 사용하여 서식이 지정되며 형식은 다음과 같습니다.

[-][d.]hh:mm:ss[.fffffff]

대괄호([ 및 ])의 요소는 반환된 문자열에 포함되지 않을 수 있습니다. 콜론 및 마침표(: 및)는 리터럴 문자입니다. 리터럴이 아닌 요소는 다음 표에 나열되어 있습니다. 메서드에서 반환 ToString() 된 문자열은 문화권을 구분하지 않습니다.

항목 설명
"-" 음수 시간 간격을 나타내는 빼기 기호입니다. 양수 시간 범위의 기호는 포함되지 않습니다.
"d" 시간 간격의 일 수입니다. 시간 간격이 1일 미만인 경우 이 요소는 생략됩니다.
"hh" 0에서 23까지의 시간 간격의 시간 수입니다.
"mm" 0에서 59까지의 시간 간격의 분 수입니다.
"ss" 시간 간격의 초(0~59)입니다.
"fffffff" 시간 간격의 소수 자릿수 초입니다. 시간 간격에 소수 자릿수 초가 포함되지 않은 경우 이 요소는 생략됩니다. 있는 경우 소수 자릿수 초는 항상 7자리 소수 자릿수를 사용하여 표현됩니다.

호출자 참고

서식 TimeSpan 값에 대한 지원이 .NET Framework 4에 추가되었습니다. 그러나 ToString() 메서드 오버로드는 문화권을 구분하지 않습니다. 해당 동작은 이전 버전의 .NET Framework 변경되지 않은 상태로 유지됩니다. 값의 서식을 TimeSpan 제어하려면 또는 ToString(String, IFormatProvider) 오버로드를 ToString(String) 호출합니다.

추가 정보

적용 대상