次の方法で共有


方法 : Calendar Web サーバー コントロールに今日の日付をプログラムで設定する

更新 : 2007 年 11 月

既定では、Calendar コントロールの "今日" の値には、Web フォーム ページを実行しているサーバーの日付が設定されます。異なるタイム ゾーンでページを閲覧する場合を考慮して、この日付を調整することもできます。

今日の日付をプログラムで設定するには

  • Calendar コントロールの TodaysDate プロパティを DateTime 値に設定します。

    次に示すのは、TodaysDate に翌日を設定し、SelectedDateTodaysDate に設定する例です。ブラウザでは、翌日の日付が強調表示されます。

    Dim tomorrow As Date = Date.Today.AddDays(1)
    Calendar1.TodaysDate = tomorrow
    Calendar1.SelectedDate = Calendar1.TodaysDate
    
    DateTime tomorrow = DateTime.Today.AddDays(1);
    Calendar1.TodaysDate = tomorrow;
    Calendar1.SelectedDate = Calendar1.TodaysDate;
    

    DropDownList コントロールに日付の選択を読み込み、リストからのユーザーの選択に従って Calendar コントロールに今日の日付値を設定する方法の例を次に示します。

    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim today As DateTime = System.DateTime.Today
            Dim yesterday As DateTime = today.AddDays(-1)
            Dim tomorrow As DateTime = today.AddDays(1)
            DropDownList1.items.Add(String.Format("{0:dd MMM yyyy}", _
               today))
            DropDownList1.items.Add(String.Format("{0:dd MMM yyyy}", _
               yesterday))
            DropDownList1.items.Add(String.Format("{0:dd MMM yyyy}", _
               tomorrow))
        End If
    End Sub
    
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender _
            As Object, ByVal e As System.EventArgs) _
            Handles DropDownList1.SelectedIndexChanged
        Calendar1.TodaysDate = _
            Date.Parse(DropDownList1.SelectedItem.Text)
    End Sub
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime today = System.DateTime.Today;
            DateTime yesterday = today.AddDays(-1);
            DateTime tomorrow = today.AddDays(1);
            DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
               today));
            DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
               yesterday));
            DropDownList1.Items.Add(String.Format("{0:dd MMM yyyy}",
               tomorrow));
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, 
        EventArgs e)
    {
        Calendar1.TodaysDate = 
            DateTime.Parse(DropDownList1.SelectedItem.Text);
    }
    

参照

概念

Calendar Web サーバー コントロールの概要