DateTimeから年月日時分秒をそれぞれ取得するサンプル

  1. using System;
  2. namespace ConsoleApplication1
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             DateTime dt = new DateTime(2013, 4, 1, 23, 59, 2, 999);
  9.             Console.WriteLine(dt);
  10.             Console.WriteLine("年 → " + dt.Year);
  11.             Console.WriteLine("月 → " + dt.Month);
  12.             Console.WriteLine("日 → " + dt.Day);
  13.             Console.WriteLine("時 → " + dt.Hour);
  14.             Console.WriteLine("分 → " + dt.Minute);
  15.             Console.WriteLine("秒 → " + dt.Second);
  16.             Console.WriteLine("ミリ秒 → " + dt.Millisecond);
  17.             Console.ReadLine();
  18.         }
  19.     }
  20. }

image

実行ファイルダウンロード

戻る