C#에서 TimeSpan을 형식이 지정된 문자열로 변환
이 게시물은 변환하는 방법에 대해 설명합니다. TimeSpan
개체를 C#의 형식이 지정된 문자열로 변환합니다.
ㅏ TimeSpan 개체는 특정 날짜와 관련이 없는 시간 간격을 나타냅니다. 와 다릅니다 DateTime
날짜 및 시간 값을 모두 나타내는 개체입니다.
우리는 얻을 수 있습니다 TimeSpan
2를 빼서 객체 DateTime
C#의 개체. 다음 예제는 TimeSpan
물체.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = new DateTime(2017, 12, 25); TimeSpan ts = now - then; Console.WriteLine("The time difference is: {0}", ts.ToString()); } } /* 결과: The time difference is: 743.20:24:39.5120760 */ |
형식을 지정하려면 TimeSpan
물건, 사용 TimeSpan.ToString()
방법. 봐봐 TimeSpan 사용자 지정 형식 문자열에 대한 Microsoft 설명서 자세한 내용은.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddHours(-2).AddMinutes(-30); TimeSpan ts = now - then; string format = @"dd\:hh\:mm\:ss\.fffffff"; Console.WriteLine("The time difference is: {0}", ts.ToString(format)); } } /* 결과: The time difference is: 00:02:29:59.9991998 */ |
그만큼 TimeSpan
개체는 일, 시간, 분, 초 및 초의 분수를 반환하도록 해당 구성원을 노출합니다. 다음 예는 다음의 속성을 표시합니다. TimeSpan
두 날짜의 차이를 나타내는 개체입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = new DateTime(2017, 12, 25); TimeSpan ts = now - then; Console.WriteLine(@"{0} Days, {1} Hours, {2} Minutes, {3} Seconds and {4} Milliseconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds); } } /* 결과: 743 Days, 20 Hours, 26 Minutes, 15 Seconds and 84 Milliseconds */ |
C#에서 TimeSpan을 형식이 지정된 문자열로 변환하는 것입니다.