本文介紹了如何確定給定日期是否是該月的第 N 個工作日?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
這是我想要做的:給定日期、星期幾和整數(shù)n
,判斷日期是否是該月的第n
天.
Here is what I am trying to do:
Given a date, a day of the week, and an integer n
, determine whether the date is the n
th day of the month.
例如:
1/1/2009,Monday,2
的輸入將是錯誤的,因為1/1/2009
不是第二個星期一
input of
1/1/2009,Monday,2
would be false because1/1/2009
is not the second Monday
輸入2008 年 11 月 13 日,星期四,2
將返回 true,因為這是第二個星期四
input of
11/13/2008,Thursday,2
would return true because it is the second Thursday
如何改進這個實現(xiàn)?
private bool NthDayOfMonth(DateTime date, DayOfWeek dow, int n)
{
int d = date.Day;
return date.DayOfWeek == dow && (d/ 7 == n || (d/ 7 == (n - 1) && d % 7 > 0));
}
推薦答案
你可以改變一周的檢查,這樣函數(shù)就會變成:
You could change the check of the week so the function would read:
private bool NthDayOfMonth(DateTime date, DayOfWeek dow, int n){
int d = date.Day;
return date.DayOfWeek == dow && (d-1)/7 == (n-1);
}
除此之外,它看起來還不錯且高效.
Other than that, it looks pretty good and efficient.
這篇關(guān)于如何確定給定日期是否是該月的第 N 個工作日?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!