2017-03-29 17 views
1

javascriptとPHP scriptが、今年の月の現在の週を取得しています。それは素晴らしいですが、少し混乱しています。私はそれが1年ではなく今月の1,2,3,4かどうかを知りたいと思います。それについてどうすればいいですか?当月の現在の週を取得する

javascriptの

/** 
* Returns the week number for this date. dowOffset is the day of week the week 
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday), 
* the week returned is the ISO 8601 week number. 
* @param int dowOffset 
* @return int 
*/ 
Date.prototype.getWeek = function (dowOffset) { 
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */ 

    dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero 
    var newYear = new Date(this.getFullYear(),0,1); 
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on 
    day = (day >= 0 ? day : day + 7); 
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1; 
    var weeknum; 
    //if the year starts before the middle of a week 
    if(day < 4) { 
     weeknum = Math.floor((daynum+day-1)/7) + 1; 
     if(weeknum > 52) { 
      nYear = new Date(this.getFullYear() + 1,0,1); 
      nday = nYear.getDay() - dowOffset; 
      nday = nday >= 0 ? nday : nday + 7; 
      /*if the next year starts before the middle of 
       the week, it is week #1 of that year*/ 
      weeknum = nday < 4 ? 1 : 53; 
     } 
    } 
    else { 
     weeknum = Math.floor((daynum+day-1)/7); 
    } 
    return weeknum; 
}; 

使用

var mydate = new Date(2011,2,3); // month number starts from 0 
// or like this 
var mydate = new Date('March 3, 2011'); 
alert(mydate.getWeek()); 
+0

試しましたか?私はこれらの2つのスクリプトを取って、あなたにコードを書くように依頼するのではなく、あなたの問題を解決する試みを少なくとも与えることをお勧めします。 – Tony

+0

@トニー:今何か試して。 –

+0

@トニー:downvoteを削除するといいですね。 –

答えて

1

2017年3月2日(木曜日)は、2017年3月には週1である2017年2月の第4週にあり、月曜日6ので、月の数はISO週は、月曜日に基づいています3月の

したがって、アルゴリズムは前の月曜日に移動し、月の月曜日を確認することです。私はこれが重複していると思ったでしょうが、私はそれを見つけることができないので、ここでは関数です。

/* Get ISO week in month, based on first Monday in month 
 
** @param {Date} date - date to get week in month of 
 
** @returns {Object} month: month that week is in 
 
**     week: week in month 
 
*/ 
 
function getISOWeekInMonth(date) { 
 
    // Copy date so don't affect original 
 
    var d = new Date(+date); 
 
    if (isNaN(d)) return; 
 
    // Move to previous Monday 
 
    d.setDate(d.getDate() - d.getDay() + 1); 
 
    // Week number is ceil date/7 
 
    return {month: +d.getMonth()+1, 
 
      week: Math.ceil(d.getDate()/7)}; 
 
} 
 
//* 
 
[new Date(2017,2,2), // Thu 2 Mar 2017 
 
new Date(2017,2,6), // Mon 6 Mar 2017 
 
new Date(2017,4,31), // Wed 31 May 2017 
 
new Date()].forEach( // Current date 
 
    function(date) { 
 
    console.log(date.toString() + '\nis in week ' + 
 
    getISOWeekInMonth(date).week + ' of month ' + 
 
    getISOWeekInMonth(date).month); 
 
    } 
 
); 
 
//*/

あなたは年にISO週取得したい場合は、Get week of year in JavaScript like in PHPは、あなたの答えを持っています。

+0

シンプルでダイナミックなコード(ハー​​ドコードされていない日付)を使用する方法を教えてもらえますか?私は自分のコードのような入力で印刷するために整数だけが必要です。私は答えとしてあなたをマークします。 –

+0

@ SebastianFarham-機能はコードの4行だけです、私はそれが十分に簡単だと思います。それはあなたがそれに渡したい任意の日付を使用します。ちょうど週番号が必要な場合は、 'Math.ceil(d.getDate()/ 7)'に戻ります。 – RobG

+0

私はこれを試しました。それから、input - > document.getElementById( 'week')を出力しようとしました。と空白。 –

0

これはどの週1から5

に知りたい方のために、月の現在の週を得ることで動作します

スクリプト

var d = new Date(); 
var date = d.getDate(); 
var day = d.getDay(); 

var weekOfMonth = Math.ceil((date + 6 - day)/7); 

document.getElementById('week').value = weekOfMonth; 

マークアップ

<input type='text' id='week' value=''> 
+0

これは、0 - 4週...おそらく 'date + 6 - day 'を生成し、1 - 5を得るでしょうか? –

+0

@JaromandaX:月に4週しかない... –

+0

しかし、ほとんどの月に28日以上あるので、1 - 4は決して正確ではない(それは1年で48週を暗示する) –

関連する問題