2010-05-07 2 views
1

두 시간 간격의 차이를 어떻게 알 수 있습니까? 13 : 45 : 26.836 - 14 : 24 : 18.473처럼 "시간 : 분 : 초 : 밀리 세컨드"형식입니다. 이제 나는이 두 시간 사이의 시간차를 찾아야합니다.두 시간 간격의 차이점을 찾을 때 문제가 발생했습니다.

C#에서 어떻게 할 수 있습니까?

미리 감사드립니다.

+0

프레임 워크의 언어는 무엇입니까? –

+0

언어 : VS2005의 C# – SyncMaster

답변

4

는 투입 그 시간 값은 DateTime 구조체에 저장됩니다. 당신이 당신이 개 DateTime 변수가 있으면, 단지 서로를 빼기 - 결과가 유형 TimeSpan의 변수입니다 : - 장치의 모든 종류의 차이, 예를 들어,

DateTime dt1 = new DateTime(2010, 5, 7, 13, 45, 26, 836); 
DateTime dt2 = new DateTime(2010, 5, 7, 14, 24, 18, 473); 

TimeSpan result = dt2 - dt1; 
string result2 = result.ToString(); 

타임 스팬이 세트를 얻을 속성의 톤을 가지고 밀리 초, 초, 분 등. .ToString()을 실행하여 결과의 ​​문자열 표현을 가져올 수도 있습니다.

00:38:51.6370000 

은 당신이 찾고있는 무엇인가요 : result2에서, 당신은 이런 식으로 뭔가를 얻을 것이다?

0

초 수를 찾습니다. 두 숫자를 뺀 다음 시차를 알아낼 수 있습니다. 사용하는 프로그래밍 언어에 따라, 나는 그것을 처리 할 수있는 라이브러리 여야한다는 점에 긍정적이다.

1

예를 게시 중입니다. 이 결과 생성합니다

/* Read the initial time. */ 
    DateTime startTime = DateTime.Now; 
    Console.WriteLine(startTime); 

    /* Do something that takes up some time. For example sleep for 1.7 seconds. */ 
    Thread.Sleep(1700); 

    /* Read the end time. */ 
    DateTime stopTime = DateTime.Now; 
    Console.WriteLine(stopTime); 

    /* Compute the duration between the initial and the end time. 
    * Print out the number of elapsed hours, minutes, seconds and milliseconds. */ 
    TimeSpan duration = stopTime - startTime; 
    Console.WriteLine("hours:" + duration.Hours); 
    Console.WriteLine("minutes:" + duration.Minutes); 
    Console.WriteLine("seconds:" + duration.Seconds); 
    Console.WriteLine("milliseconds:" + duration.Milliseconds); 
0
//Start off with a string 
string time1s = "13:45:26.836"; 
string time2s = "14:24:18.473"; 

TimeSpan interval = DateTime.Parse(time2s) - DateTime.Parse(time1s); 

당신이 그것을 확인하고 프로그램을 적용 할 수 있습니다 : 당신이해야 할 일을 기본적으로

Days 0 int   Hours 0 int 
    Milliseconds 637 int 
    Minutes 38 int   Seconds 51 int 
    Ticks 23316370000 long 
    TotalDays 0.02698653935185185 double 
    TotalHours 0.64767694444444446 double 
    TotalMilliseconds 2331637.0 double 
    TotalMinutes 38.860616666666665 double 
    TotalSeconds 2331.6369999999997 double 
관련 문제