2010-05-26 347 views
2

在JavaScript中檢測每個(當前)月的最後一週的方法是什麼?或者本月的最後一個星期一?使用javascript檢測每個月的最後一週使用javascript

+0

+1好問題,我即將寫一個自定義日期選擇器,並開始問自己類似的問題。 – 2010-05-26 15:00:52

+1

這是上個星期是在一個月還是在這個月的最後一週?你的星期是星期天還是星期一開始? – Skilldrick 2010-05-26 15:02:50

+0

他確實提到*本月的最後一個星期一* .. – 2010-05-26 15:35:33

回答

0

使用getDay()可以得到當月的最後一天的星期幾,並從中獲得工作(減去該月的天數應該可以做到這一點)。

0

要確定它是否是星期一,請使用.getDay() == 1。要確定它是否是最後一個月,請添加七天並比較幾個月:nextMonday.setDate(monday.getDate()+7); nextMonday.getMonth() == monday.getMonth();

0

Javascript「Date」對象是您的朋友。

function lastOfThisMonth(whichDay) { 
    var d= new Date(), month = d.getMonth(); 
    d.setDate(1); 
    while (d.getDay() !== whichDay) d.setDate(d.getDate() + 1); 
    for (var n = 1; true; n++) { 
    var nd = new Date(d.getFullYear(), month, d.getDate() + n * 7); 
    if (nd.getMonth() !== month) 
     return new Date(d.getFullYear(), month, d.getDate() + (n - 1) * 7).getDate(); 
    } 
} 

這會給你這一週的選擇一天(0〜7)該月的最後一天的日期(月,好像是30)。

尋找最後本週的月份將取決於你的意思。如果你的意思是最後一個完成周,那麼(如果你的意思是星期天 - 星期六)找到上個星期六,並減去6.如果你的意思是上個星期在本月開始,找到上個星期天。

+0

顯然沒有必要循環查找最後的日期;一個簡單的部門會弄清楚這一點,但我太懶惰/愚蠢的想弄清楚它的樣子。 – Pointy 2010-05-26 15:43:06

+0

我真的不喜歡穿過天,希望我們可以避免它。 – devjs11 2010-05-26 16:28:11

+0

至少它每次跳過7次:)讓我考慮一下。 – Pointy 2010-05-26 16:32:43

1

date對象及其方法,你可以做以下的播放..

更新

完整的計算才能獲得到該月的最後一個星期一可能被壓縮到

var d = new Date(); 
d.setMonth(d.getMonth() + 1); 
d.setDate(0); 
lastmonday = d.getDate() - (d.getDay() - 1); 
alert(lastmonday); 

詳細示例..

var now = new Date(); // get the current date 

// calculate the last day of the month 
if (now.getMonth() == 11) // if month is dec then go to next year and first month 
{ 
    nextmonth = 0; 
    nextyear = now.getFullYear() + 1; 
} 
else // otherwise go to next month of current year 
{ 
    nextmonth = now.getMonth() + 1; 
    nextyear = now.getFullYear(); 
} 

var d = new Date(nextyear , nextmonth , 0); // setting day to 0 goes to last date of previous month 
alert(d.getDay()); // will alert the day of the week 0 being sunday .. you can calculate from there to get the first day of that week .. 
+0

看起來像一個工作和非常短的解決方案,確實返回上週一,將測試更多,任何人都認爲相同的方式? – devjs11 2010-05-26 20:59:21

+0

好的,我發現該代碼存在問題。由於某種原因,如果該月的最後一天實際上是星期一,那麼它會在下個月的最後一個星期一發出警報。 – devjs11 2010-05-27 10:34:08

+0

@Alex,本月(2010年5月)在本月的最後一天(2010年5月31日)確實有星期一。和代碼如果運行警報31 ...你可以給另一個例子(*你發現是錯誤*),所以我可以測試它並找出問題? – 2010-05-27 12:07:54

1

我建議(1)或星期日(0)。基於這一週什麼時候上獲得天的月份,並從最後一天的號碼,然後循環,直到getDay()還給星期一開始。一旦你的起始日期......結束日期會的startDate + 7這樣的東西沿着這些線路

我發現this有所幫助:

//Create a function that determines how many days in a month 
//NOTE: iMonth is zero-based .. Jan is 0, Feb is 2 and so on ... 
function daysInMonth(iMonth, iYear) 
{ 
    return 32 - new Date(iYear, iMonth, 32).getDate(); 
} 

然後循環:

//May - should return 31 
var days_in_month = daysInMonth(4, 2010); 

var weekStartDate = null; 
var weekEndDate = null;  

for(var i=days_in_month; i>0; i--) 
{ 
    var tmpDate = new Date(2010, 4, i); 
    //week starting on sunday 
    if(tmpDate.getDay() == 0) 
    { 
    weekStartDate = new Date(tmpDate); 
    weekEndDate = new Date(tmpDate.setDate(tmpDate.getDate() + 6)); 
    //break out of the loop 
    break; 
    } 
} 
0

您可能還希望在給定日期之前或之後查找第三個星期一或第一個星期二 或在每個星期三的兩個日期之間標記。

Date.prototype.lastweek= function(wd, n){ 
    n= n || 1; 
    return this.nextweek(wd, -n); 
} 
Date.prototype.nextweek= function(wd, n){ 
    if(n== undefined) n= 1; 
    var incr= (n<0)? 1: -1, 
    D= new Date(this), 
    dd= D.getDay(); 
    if(wd=== undefined) wd= dd; 
    if(dd!= wd) while(D.getDay()!= wd) D.setDate(D.getDate()+incr); 
    D.setDate(D.getDate()+7*n); 
    D.setHours(0, 0, 0, 0); 
    return D; 
} 


function lastMondayinmonth(month, year){ 
    var day= new Date(); 
    if(!month) month= day.getMonth()+1; 
    if(!year) year= day.getFullYear(); 
    day.setFullYear(year, month, 0); 
    return day.lastweek(1); 
} 
alert(lastMondayinmonth()) 
0

我發現,檢測每星期的最後一個星期一,但它不會檢測到該月的最後一個星期一這樣的例子。也許這將有助於找到更好的解決方案,代碼看起來很短。

var dif, d = new Date(); // Today's date 
dif = (d.getDay() + 6) % 7; // Number of days to subtract 
d = new Date(d - dif * 24*60*60*1000); // Do the subtraction 

alert(d); // Last monday. 
0

獲取該月的最後一天:

/** 
* Accepts either zero, one, or two parameters. 
*  If zero parameters: defaults to today's date 
*  If one parameter: Date object 
*  If two parameters: year, (zero-based) month 
*/ 
function getLastDay() { 
    var year, month; 
    var lastDay = new Date(); 

    if (arguments.length == 1) { 
     lastDay = arguments[0]; 
    } else if (arguments.length > 0) { 
     lastDay.setYear(arguments[0]); 
     lastDay.setMonth(arguments[1]); 
    } 

    lastDay.setMonth(lastDay.getMonth() + 1); 
    lastDay.setDate(0); 

    return lastDay; 
} 

獲得的最後一個星期一:

/** 
* Accepts same parameters as getLastDay() 
*/ 
function getLastMonday() { 
    var lastMonday = getLastDay.apply(this, arguments); 
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay() == 0 ? 6 : (lastMonday.getDay() - 1))); 
    return lastMonday; 
} 

獲取本週一年的某一天:

/** 
* Accepts one parameter: Date object. 
* Assumes start of week is Sunday. 
*/ 
function getWeek(d) { 
    var jan1 = new Date(d.getFullYear(), 0, 1); 
    return Math.ceil((((d - jan1)/(24 * 60 * 60 * 1000)) + jan1.getDay() + 1)/7); 
} 

把它們放在一起(假設你使用的是Firebug):

// Get the last day of August 2006: 
var august2006 = new Date(2006, 7); 
var lastDayAugust2006 = getLastDay(august2006); 
console.log("lastDayAugust2006: %s", lastDayAugust2006); 

// ***** Testing getWeek() ***** 
console.group("***** Testing getWeek() *****"); 
    // Get week of January 1, 2010 (Should be 1): 
    var january12010Week = getWeek(new Date(2010, 0, 1)); 
    console.log("january12010Week: %s", january12010Week); 

    // Get week of January 2, 2010 (Should still be 1): 
    var january22010Week = getWeek(new Date(2010, 0, 2)); 
    console.log("january22010Week: %s", january22010Week); 

    // Get week of January 3, 2010 (Should be 2): 
    var january32010Week = getWeek(new Date(2010, 0, 3)); 
    console.log("january32010Week: %s", january32010Week); 
console.groupEnd(); 
// ***************************** 

// Get the last week of this month: 
var lastWeekThisMonth = getWeek(getLastDay()); 
console.log("lastWeekThisMonth: %s", lastWeekThisMonth); 

// Get the last week of January 2007: 
var lastWeekJan2007 = getWeek(getLastDay(2007, 0)); 
console.log("lastWeekJan2007: %s", lastWeekJan2007); 

// Get the last Monday of this month: 
var lastMondayThisMonth = getLastMonday(); 
console.log("lastMondayThisMonth: %s", lastMondayThisMonth); 

// Get the week of the last Monday of this month: 
var lastMondayThisMonthsWeek = getWeek(lastMondayThisMonth); 
console.log("lastMondayThisMonthsWeek: %s", lastMondayThisMonthsWeek); 
0

好吧,到目前爲止,我想出了這樣的解決方案,使它有點我自己的方式,並得到了一些在這裏提到的東西。它工作正確,並始終返回當前月份的最後一個星期一。

//function that will help to check how many days in month 
function daysInMonth(iMonth, iYear) 
{ 
    return 32 - new Date(iYear, iMonth, 32).getDate(); 
} 


var dif = null; 
d = new Date(); // Today's date 
countDays = daysInMonth(d.getMonth(),d.getFullYear()); //Checking number of days in current month 
d.setDate(countDays); //setting the date to last day of the month 
dif = (d.getDay() + 6) % 7; // Number of days to subtract 
d = new Date(d - dif * 24*60*60*1000); // Do the subtraction 
alert(d.getDate()); //finally you get the last monday of the current month 
相關問題