2012-02-21 43 views

回答

1

您可以使用

DateTime.Now.DayOfWeek 

要提取一週的當天:

DayOfWeek dToday = DateTime.Now.DayOfWeek; 
int iDay = dToday.GetHashCode(); // a number between 0-6 representing 
           // the days in the week 
string sDayName = dToday.ToString(); // can be either Sunday, Monday .. Satruday 
2

嗯,你可以很容易地找到了天是否匹配:

// Note: consider time zones... 
DateTime today = DateTime.Today; 

if (today.DayOfWeek == DayOfWeek.Monday) 
{ 
    ... 
} 

,你知道,一週的每一天中第一次出現將在1-7範圍內,第二個將在8-14範圍等於是:

// Check if it's the second Friday of the month... 
int targetOccurrence = 2; 
DayOfWeek targetDay = DayOfWeek.Friday; 

DateTime today = DateTime.Today; 
if (today.DayOfWeek == targetDay && 
    (today.Day + 6)/7 == targetOccurrence) 
{ 
    // Yes, it's the second Friday 
} 

如果你想找出它是否之前或每月的第二個星期五之後,這是稍硬。不是以任何方式不可能的,但更加煩瑣。

0

使用DateTime.DayOfWeek屬性。

例如

if (DateTime.Now.DayOfWeek == DayOfWeek.Monday) 
{ 
    Console.WriteLine("Someone's got a case of the Mondays!"); 
} 
相關問題