C#の - のDateTime与のTimeSpan

1.日時の使用

    日時DT = DateTime.Now;

    StringBuilderのSB = 新しいStringBuilderの();
    sb.AppendLine(文字列 .Format(" DataTime列表現:{0} " 、dt.ToString()))。

    sb.AppendLine(文字列 .Format(" DataTimeの年の部分:{0} " 、dt.Year))。
    sb.AppendLine(文字列 .Format(" DataTimeの月の部分:{0} " 、dt.Month))。
    sb.AppendLine(文字列 .Format(" DataTime日付部分:{0} " 、dt.Date))。
    sb.AppendLine(文字列 .Format(" DataTimeの時間部分:{0} " 、dt.Hour))。
    sb.AppendLine(文字列 .Format(" DataTimeの微小部分:{0} " 、dt.Minute))。
    sb.AppendLine(文字列 .Format(" DataTimeの第二部:{0} " 、dt.Second))。
    sb.AppendLine(文字列 .Format(" DataTimeのミリ秒部分:{0} " 、dt.Millisecond))。

    sb.AppendLine(文字列 .Format(" 月のDataTime日である:{0} " 、dt.Day))。
    sb.AppendLine(文字列 .Format(" DataTime今週の日である:{0} " )、dt.DayOfWeek)。
    sb.AppendLine(文字列 .Format(" DataTimeは、その年の日である:{0} " 、dt.DayOfYear))。
    sb.AppendLine(文字列 .Format(" 日のDataTime取得時間部分(のTimeSpan形式):{0} " 、dt.TimeOfDay))。

    sb.AppendLine(文字列 .Format(" 日時が64ビットの2進数に変換:{0} " 、dt.ToBinary()))。
    sb.AppendLine(文字列 .Format(" Windowsファイル時刻に日時:{0} " 、dt.ToFileTime()));
    sb.AppendLine(文字列 .Format(" 日付と時刻の変換UTC Windowsファイル時刻:{0} " 、dt.ToFileTimeUtc()));
    sb.AppendLine(文字列 .Format(" 日時がローカル時刻に変換:{0} " 、dt.ToLocalTime()))。
    sb.AppendLine(文字列 .Format(" 日時は、OLEオートメーション日付に変換:{0} " 、dt.ToOADate()))。
    sb.AppendLine(文字列 .Format(" 日時コンバートUTC時間:{0} " 、dt.ToUniversalTime()))。

    sb.AppendLine(文字列 .Format(" 長い形式のDateTime日付を変換:{0} " 、dt.ToLongDateString()))。
    sb.AppendLine(文字列 .Format(" 日時が短い日付の形式に変換:{0} " 、dt.ToShortDateString()))。
    sb.AppendLine(文字列 .Format(" 日時が長い形式に変換:{0} " 、dt.ToLongTimeString()))。
    sb.AppendLine(文字列 .Format(" 日時が短い形式に変換:{0} " 、dt.ToShortTimeString()))。

    Console.WriteLineを(sb.ToString())。

 

2. TimeSpanの使用

    日時dtStartTime = Convert.ToDateTime(" 2019年1月1日夜12時○○分00秒" )。
    日時dtEndTime = Convert.ToDateTime(" 2019年12月31日夜11時59分59秒" )。

    TimeSpanのTS = dtEndTime - dtStartTime。

    StringBuilderのSB = 新しいStringBuilderの();
    sb.AppendLine(文字列 .Format(" 文字列は時間間隔を表す:{0} " 、ts.ToString()))。

    sb.AppendLine(文字列 .Format(" インターバル部日:{0} " 、ts.Days))。
    sb.AppendLine(文字列 .Format(" 時間間隔時間部分:{0} " 、ts.Hours))。
    sb.AppendLine(文字列 .Format(" 間隔分の部分:{0} " 、ts.Minutes))。
    sb.AppendLine(文字列 .Format(" 時間間隔の第二成分:{0} " 、ts.Seconds))。
    sb.AppendLine(文字列 .Format(" 時間間隔ミリ秒部:{0} " 、ts.Milliseconds))。

    sb.AppendLine(文字列 .Format(" 日数が表される間隔:{0} " 、ts.TotalDays))。
    sb.AppendLine(文字列 .Format(" インターバル時間内で発現:{0} " 、ts.TotalHours))。
    sb.AppendLine(文字列 .Format(" 分の時間間隔を表す:{0} "をts.TotalMinutes))。
    sb.AppendLine(文字列 .Format(" 秒で表される時間間隔を有する:{0} " 、ts.TotalSeconds))。
    sb.AppendLine(文字列 .Format(" ミリ秒の時間間隔:{0} " 、ts.TotalMilliseconds))。

    Console.WriteLineを(sb.ToString())。

おすすめ

転載: www.cnblogs.com/ChengWenHao/p/CSharpDateTimeAndTimeSpan.html