728x90
반응형
WPF & C# - DateTime과 TimeSpan ( 시간 설정 / 이동 / 요일 / 셋팅 / UTC / 포멧 / ParseExact / 변환 ) |
MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <StackPanel> <!--DateTime--> <TextBlock Text="DateTime ( 정해진 시각 )" FontWeight="Bold" FontSize="18" Margin="10"></TextBlock> <WrapPanel Orientation="Horizontal"> <!-- 시간설정 --> <StackPanel Margin="10"> <Button x:Name="btn00" Content="시간설정" Height="30" Click="btn00_Click" Margin="5"/> <Label x:Name="lbl00" Margin="5"></Label> </StackPanel> <!-- 이동 --> <StackPanel Margin="10"> <Button x:Name="btn01" Content="이동" Height="30" Click="btn01_Click" Margin="5"/> <Label x:Name="lbl01" Margin="5"></Label> </StackPanel> <!-- 요일 --> <StackPanel Margin="10"> <Button x:Name="btn03" Content="요일" Height="30" Margin="5" Click="btn03_Click"/> <Label x:Name="lbl03" Margin="5"></Label> </StackPanel> <!-- Local / UTC --> <StackPanel Margin="10"> <Button x:Name="btn04" Content="Local(+9시간) / UTC" Height="30" Margin="5" Click="btn04_Click"/> <Label x:Name="lbl04" Margin="5"></Label> </StackPanel> <!-- Format 포멧 변경 --> <StackPanel Margin="10"> <Button x:Name="btn02" Content="DateTime > String (ToString)" Height="30" Margin="5" Click="btn02_Click"/> <Label x:Name="lbl02" Margin="5"></Label> </StackPanel> <!-- ParseExact(포멧/형식) --> <StackPanel Margin="10"> <Button x:Name="btn05" Content="String > DateTime (ParseExact)" Height="30" Margin="5" Click="btn05_Click"/> <Label x:Name="lbl05" Margin="5"></Label> </StackPanel> </WrapPanel> <!--TimeSpan--> <TextBlock Text="TimeSpan ( 시간의 크기)" FontWeight="Bold" FontSize="18" Margin="10"></TextBlock> <WrapPanel Orientation="Horizontal"> <!-- TimeSpan 기본값 --> <StackPanel Margin="10"> <Button x:Name="btn07" Content="TimeSpan 기본값" Height="30" Margin="5" Click="btn07_Click"/> <Label x:Name="lbl07" Margin="5"></Label> </StackPanel> <!-- 시간 합산하기 --> <StackPanel Margin="10"> <Button x:Name="btn06" Content="시간 합산하기 TimeSpan(1, 2, 3)" Height="30" Margin="5" Click="btn06_Click"/> <Label x:Name="lbl06" Margin="5"></Label> </StackPanel> <!-- 시간 변환 --> <StackPanel Margin="10"> <Button x:Name="btn08" Content="시간 변환(일일)" Height="30" Margin="5" Click="btn08_Click"/> <Label x:Name="lbl08" Margin="5"></Label> </StackPanel> </WrapPanel> </StackPanel> | cs |
MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | // 날짜 및 시간 설정 private void btn00_Click(object sender, RoutedEventArgs e) { lbl00.Content = DateTime.Now + "\n"; // 현재시간 lbl00.Content += default(DateTime).ToString(); // 시간 기본값 (초기화) } // 날짜 및 시간 이동 private void btn01_Click(object sender, RoutedEventArgs e) { DateTime dt = DateTime.Now; //DateTime.Now; // 현재시간 DateTime tomorrow = dt.AddDays(1); // 하루 뒤 DateTime yesterday = dt.AddDays(-1); // 하루 전 DateTime NextHour = dt.AddHours(1); // 한시간 뒤 lbl01.Content = tomorrow.ToString(); lbl01.Content += "\n" + yesterday.ToString(); lbl01.Content += "\n" + NextHour.ToString(); } // 요일 private void btn03_Click(object sender, RoutedEventArgs e) { DateTime dt = DateTime.Now; //DateTime.Now; // 현재시간 lbl03.Content = dt.DayOfWeek; } // Local / UTC private void btn04_Click(object sender, RoutedEventArgs e) { DateTime dt = DateTime.Now; //DateTime.Now; // 현재시간 lbl04.Content = dt.ToLocalTime() + "\n"; lbl04.Content += dt.ToUniversalTime().ToString(); } // Format 포멧 private void btn02_Click(object sender, RoutedEventArgs e) { DateTime dt = new DateTime(2018, 09, 01, 15, 16, 17, DateTimeKind.Local); //DateTime.Now; // 현재시간 lbl02.Content = String.Format(dt.ToString("yyyy년MM월dd일 tthh시mm분ss.fffffff초")) + "\n"; lbl02.Content += String.Format(dt.ToString("yyyy년MM월dd일 HH시mm분ss.fffffff초")); // tt : (AM/PM), hh/HH } // ParseExact(포멧/형식) private void btn05_Click(object sender, RoutedEventArgs e) { string date = "20180828"; DateTime dt = DateTime.ParseExact(date, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); lbl05.Content = date; lbl05.Content += dt.ToString(); } // TimeSpan 기본값 private void btn07_Click(object sender, RoutedEventArgs e) { lbl07.Content = default(TimeSpan); } // 시간 합산하기 private void btn06_Click(object sender, RoutedEventArgs e) { DateTime dt = DateTime.Now; TimeSpan ts = new TimeSpan(1, 2, 3); lbl06.Content = dt + "\n"; lbl06.Content += (dt - ts).ToString(); } // 시간 변환 private void btn08_Click(object sender, RoutedEventArgs e) { DateTime dt = DateTime.Now; TimeSpan tod = dt.TimeOfDay; lbl08.Content = dt + "\n"; lbl08.Content += tod.TotalMinutes.ToString() + "분"; } | cs |
728x90
반응형