在JavaScript中檢測每個(當前)月的最後一週的方法是什麼?或者本月的最後一個星期一?使用javascript檢測每個月的最後一週使用javascript
回答
使用getDay()
可以得到當月的最後一天的星期幾,並從中獲得工作(減去該月的天數應該可以做到這一點)。
要確定它是否是星期一,請使用.getDay() == 1
。要確定它是否是最後一個月,請添加七天並比較幾個月:nextMonday.setDate(monday.getDate()+7);
nextMonday.getMonth() == monday.getMonth();
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.如果你的意思是上個星期在本月開始,找到上個星期天。
與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 ..
我建議(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;
}
}
您可能還希望在給定日期之前或之後查找第三個星期一或第一個星期二 或在每個星期三的兩個日期之間標記。
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())
我發現,檢測每星期的最後一個星期一,但它不會檢測到該月的最後一個星期一這樣的例子。也許這將有助於找到更好的解決方案,代碼看起來很短。
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.
獲取該月的最後一天:
/**
* 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);
好吧,到目前爲止,我想出了這樣的解決方案,使它有點我自己的方式,並得到了一些在這裏提到的東西。它工作正確,並始終返回當前月份的最後一個星期一。
//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
- 1. 如何檢測使用Javascript的最後2個單詞?
- 2. 刪除最後一個使用JavaScript
- 3. 如何使用javascript獲取當月的最後一天?
- 4. 使用javascript獲取數組中的最後一項使用javascript
- 5. 僅返回每個月的最後一天,使用SQL
- 6. 檢測中使用JavaScript
- 7. 使用Javascript檢測Safari 5
- 8. 使用JavaScript檢測時區
- 9. 檢測輸入使用JavaScript
- 10. 使用Javascript檢測IE8 64bit
- 11. 使用javascript檢測HTML5
- 12. 使用PHP或javascript檢測服務器的每個請求
- 13. 使用Javascript和/或jQuery將每週數據轉換爲每月數據
- 14. 查詢使用oracle sql查找每年的每個月的最後一週的日期sql
- 15. 使用jQuery或JavaScript對一週,一個月和一年進行排序日期
- 16. 周的財季,使用JavaScript
- 17. 顯示使用Javascript一週的元素
- 18. 如何使用javascript檢測javascript版本的一致性
- 19. 使用sql檢測週期
- 20. 選擇每個月的最後一天
- 21. .NET中每個月的最後一天
- 22. 檢測jQuery中的最後一個列表項/ javascript
- 23. 如何在每個月的最後一個週末一天製作觸發器
- 24. 使用javascript檢測高度自動使用javascript
- 25. 每個月的第一個週二
- 26. 使用javascript檢測瀏覽器用戶
- 27. 使用Javascript檢測URL並應用CSS
- 28. 使用JavaScript檢測用戶活動
- 29. 檢測雙擊使用javascript禁用
- 30. 使用Javascript最大化/最小化檢測瀏覽器窗口
+1好問題,我即將寫一個自定義日期選擇器,並開始問自己類似的問題。 – 2010-05-26 15:00:52
這是上個星期是在一個月還是在這個月的最後一週?你的星期是星期天還是星期一開始? – Skilldrick 2010-05-26 15:02:50
他確實提到*本月的最後一個星期一* .. – 2010-05-26 15:35:33