본문 바로가기

Development/c#

[c#] DateTime 차이 계산

반응형
환경 :  Visual Studio 2015
DateTime 차이 (시작 일시 ~ 종료 일시) 의 계산 
        public void DiffTime()
        {
            string format = "yyyyMMddHHmmss.fff";
            string strStartDateTime = "19771001080000.000";
            DateTime startDateTime = DateTime.ParseExact(strStartDateTime, format, CultureInfo.InvariantCulture);

            DateTime endDateTime = DateTime.Now;

            TimeSpan tsDiff = endDateTime - startDateTime;
            DateTime diffDateTime = DateTime.MinValue + tsDiff;

            int diffYear = diffDateTime.Year;
            int diffMonth = diffDateTime.Month;
            int diffDay = diffDateTime.Day;
            int diffHour = diffDateTime.Hour;
            int diffMinute = diffDateTime.Minute;
            int diffSecond = diffDateTime.Second;
            int diffMillisecond = diffDateTime.Millisecond;

            Debug.WriteLine(string.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}", diffYear, diffMonth, diffDay, diffHour, diffMinute, diffSecond, diffMillisecond));

        }
반응형