共用方式為


DateTime.DayOfWeek 屬性

定義

取得由這個執行個體表示的一週天數。

public:
 property DayOfWeek DayOfWeek { DayOfWeek get(); };
public DayOfWeek DayOfWeek { get; }
member this.DayOfWeek : DayOfWeek
Public ReadOnly Property DayOfWeek As DayOfWeek

屬性值

DayOfWeek

列舉的常數,表示這個 DateTime 值代表的是一週的哪一天。

範例

下列範例示範 DayOfWeek 屬性和 System.DayOfWeek 列舉。

// This example demonstrates the DateTime.DayOfWeek property
using namespace System;
int main()
{
   
   // Assume the current culture is en-US.
   // Create a DateTime for the first of May, 2003.
   DateTime dt = DateTime(2003,5,1);
   Console::WriteLine(  "Is Thursday the day of the week for {0:d}?: {1}", dt, dt.DayOfWeek == DayOfWeek::Thursday );
   Console::WriteLine(  "The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek );
}

/*
This example produces the following results:

Is Thursday the day of the week for 5/1/2003?: True
The day of the week for 5/1/2003 is Thursday.
*/
// This example demonstrates the DateTime.DayOfWeek property
using System;

class Sample
{
    public static void Main()
    {
// Assume the current culture is en-US.
// Create a DateTime for the first of May, 2003.
    DateTime dt = new DateTime(2003, 5, 1);
    Console.WriteLine("Is Thursday the day of the week for {0:d}?: {1}",
                       dt, dt.DayOfWeek == DayOfWeek.Thursday);
    Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek);
    }
}
/*
This example produces the following results:

Is Thursday the day of the week for 5/1/2003?: True
The day of the week for 5/1/2003 is Thursday.
*/
' This example demonstrates the DateTime.DayOfWeek property
Class Sample
   Public Shared Sub Main()
      ' Assume the current culture is en-US.
      ' Create a DateTime for the first of May, 2003.
      Dim dt As New DateTime(2003, 5, 1)
      Console.WriteLine("Is Thursday the day of the week for {0:d}?: {1}", _
                         dt, dt.DayOfWeek = DayOfWeek.Thursday)
      Console.WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek)
   End Sub
End Class
'
'This example produces the following results:
'
'Is Thursday the day of the week for 5/1/2003?: True
'The day of the week for 5/1/2003 is Thursday.
'

備註

列舉中的常數值 DayOfWeek 範圍從 DayOfWeek.Sunday 到到 DayOfWeek.Saturday 。 如果轉換成整數,其值的範圍為零 (,表示 DayOfWeek.Sunday) 到六個 (表示 DayOfWeek.Saturday) 。

DayOfWeek屬性會傳回列舉的常數,而不會反映系統的地區和語言設定。 若要抓取表示特定日期之當地語系化工作日名稱的字串,請呼叫包含參數之方法的其中一個多載, ToString format 並將其傳遞給 ddddddd 自訂格式字串。 如需詳細資訊,請參閱 如何:從特定日期解壓縮一周的哪一天

適用於

另請參閱