2

各(現在の)月の最後の週を検出するJavaScriptの方法は何でしょうか。それとも月末月曜日?

4

9 に答える 9

1

日付オブジェクトとそのメソッドをいじると、次のことができます..

アップデート

月の最後の月曜日に到達するための完全な計算は、次のように圧縮できます。

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 ..
于 2010-05-26T15:21:27.640 に答える
1

月の日数を取得し、最終日から getDay() が月曜日 (1) または日曜日 (0) を返すまでループすることをお勧めします.. 週の始まりに基づいて.. 開始日を取得したら...終了日はstartDate + 7になるので、これらの行に沿った何か

私はこれが役に立ちました:

//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; 
  }
}
于 2010-05-26T15:28:59.343 に答える
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を引きます。その月に始まる最後の週を意味する場合は、最後の日曜日を見つけます。

于 2010-05-26T15:07:48.073 に答える
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.
于 2010-05-26T16:27:02.117 に答える
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);
于 2010-05-26T21:47:15.240 に答える
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
于 2010-05-27T12:10:20.713 に答える
0

また、特定の日付の前後の第 3 月曜日または第 1 火曜日を検索したり、2 つの日付の間の毎週水曜日にフラグを立てたりすることもできます。

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())
于 2010-05-26T15:56:19.500 に答える
0

月の最後の日の曜日を取得し、それから作業するために使用getDay()します (月の日数から値を差し引くと、おそらくうまくいくはずです。+/- 1)。

于 2010-05-26T15:04:08.987 に答える
0

月曜日かどうかを判断するには、 を使用します.getDay() == 1。月の最後かどうかを判断するには、7 日を追加して月を比較します。nextMonday.setDate(monday.getDate()+7); nextMonday.getMonth() == monday.getMonth();

于 2010-05-26T15:07:19.000 に答える