C#
C# -- 현재 시간 구하기 & 시분초 시간을 초로 환산하기
자유프로그램
2019. 4. 11. 14:13
반응형
C# 에서 현재 시간 구하기. 시분초 시간을 초로 환산하기
참고 : https://docs.microsoft.com/ko-kr/dotnet/api/system.timespan?view=netframework-4.7.2
https://docs.microsoft.com/ko-kr/dotnet/api/system.datetime.now?view=netframework-4.7.2
DateTime.Now 사용하기
<< 결과 출력 화면 >>

--
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading; | |
| namespace datetime_test | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| DateTime currentTime = DateTime.Now; | |
| Console.WriteLine($"{currentTime}"); | |
| Console.WriteLine($"{currentTime.TimeOfDay}"); // 하루 중 자정부터 경과한 시간을 나타내는 시간 간격 | |
| Console.WriteLine($"yyyyMMdd 양식 = {currentTime.ToString("yyyyMMdd")}, 24hr hhmmss 양식 = {currentTime.ToString("HHmmss")}"); | |
| TimeSpan time = DateTime.Now.TimeOfDay; | |
| Console.WriteLine($"time.Hours = {time.Hours}"); | |
| Console.WriteLine($"time.Minutes = {time.Minutes}"); | |
| Console.WriteLine($"time.Seconds = {time.Seconds}"); | |
| Console.WriteLine($"time.Seconds = {time.TotalSeconds}"); // 시분초 시간을 모두 초로 환산한 결과. | |
| Thread.Sleep(4000); | |
| DateTime time2 = DateTime.Now; | |
| TimeSpan deltaTime = time2 - currentTime; | |
| Console.WriteLine($"시간차 = {deltaTime}"); | |
| Console.WriteLine($"시간차(sec) = Seconds=> {deltaTime.Seconds}, TotalSeconds=> {deltaTime.TotalSeconds}"); | |
| Console.WriteLine($"시간차(msec) = {deltaTime.TotalMilliseconds}"); | |
| Console.WriteLine($"{deltaTime.TotalMilliseconds - 4000}"); | |
| Console.Read(); | |
| } | |
| } | |
| } |
반응형