header
CodePanic! > C#.NET Tips > 今ここ

■曜日を取得する

DateTime構造体のDayOfWeekプロパティーを参照します。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 現在の日付と時刻を取得
            DateTime date = DateTime.Now;

            // 曜日を取得
            Console.WriteLine(date.DayOfWeek);

            // 日本語で
            string week = string.Empty;
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday: week = "日"; break;
                case DayOfWeek.Monday: week = "月"; break;
                case DayOfWeek.Tuesday: week = "火"; break;
                case DayOfWeek.Wednesday: week = "水"; break;
                case DayOfWeek.Thursday: week = "木"; break;
                case DayOfWeek.Friday: week = "金"; break;
                case DayOfWeek.Saturday: week = "土"; break;
            }

            Console.WriteLine(week + "曜日です。");
        }
    }
}

【結果】

Tuesday
火曜日です。





zFCodePress C#vO~OTips


Copyright © 2008.07 - shougo suzaki
ゥRN\Agbvy[WB