2015-01-27 7 views
1

평일의 실제 위치를 찾으려고합니다. 나는 그것을 루프와 함께 작동 시키지만 어떤 수학에서는 그것을 찾으려고 노력한다. 나는 그것을 7로 나눌 것 같지만 그것이 효과가 없다는 것을 안다.JavaScript에서 주어진 요일의 서수 위치를 찾는 방법

다음은 코드입니다.

for(var ind=0; ind<=between.length; ind++){ 
    if (new Date(between[ind]).getMonthWeek() === baseDtWk && new Date(between[ind]).getDay() === baseDtD) { 
     datesToBeMarked.push(between[ind]); 
     console.log(" :Date: " + between[ind] + " :Week: " + new Date(between[ind]).getMonthWeek()); 
     console.log("Date entered : " + new Date(between[ind])); 
    } 

} 
+0

평일의 순위는 무엇입니까? Sun = 0, Mon = 1 등을 의미합니까? – Barmar

+0

시도한 코드를 표시하십시오. – Barmar

+1

@Barmar'for (var ind = 0; ind <= between.length; ind ++) { if (ind)). getMonthWeek() === baseDtWk && new Date ([ind] 사이). getDay() === baseDtD) { datesToBeMarked.push ([ind] 사이); console.log (": 날짜 :"[ind] + "사이 : 주 :"+ 새 날짜 ([ind]). getMonthWeek()); console.log ("입력 한 날짜 :"+ 새 날짜 ([ind]) 사이); } } – Superman

답변

1

나는 며칠 전을 마쳤습니다. 아래 코드와 같이 간단합니다. :)

On fiddle.

당신은 당신이 결과를보고하는 방법을 표시하지 않은
Number.prototype.nth= function(){ 
    var n= Math.round(this), t= Math.abs(n%100), i= t%10; 
    if(i<4 && (t<4 || t> 20)){ 
     switch(i){ 
      case 1:return n+'st'; 
      case 2:return n+'nd'; 
      case 3:return n+'rd'; 
     } 
    } 
    return n+'th'; 
} 
Date.prototype.nthofMonth= function(){ 
    var today= this.getDate(),m=this.getMonth(), 
    day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 
    'Friday', 'Saturday'][this.getDay()], 
    month= ['January', 'February', 'March', 'April', 'May', 'June', 
    'July', 'August', 'September', 'October', 'November', 'December'][m]; 
    return [(m+1)+'-'+today,'the ', Math.ceil(today/7).nth(), day, 'of', month, 'in', this.getFullYear()].join(' '); 
} 

var date=new Date().nthofMonth(); 
console.log(date); 
+0

'(Math.ceil ((오늘)/7)) .nth()'는'Math.ceil (today/7) .nth()'와 같이 읽기가 훨씬 쉽습니다. 출력 문자열의 형식을 지정하기위한 배열을 작성하는 것은 매우 비효율적입니다. – RobG

0

, 난 당신이 특정 날짜, 말, n 번째 화요일 예를 들어 있는지 알고 싶은 생각

// Add ordinal to a number 
function addOrdinal(n) { 
    var ord = [,'st','nd','rd']; 
    var a = n % 100; 
    return n + (ord[a>20? a%10 : a] || 'th'); 
} 

// Return the ordinal number of a day in the month 
function ordinalDay(d) { 
    d = d || new Date(); 
    var days = ['Sunday','Monday','Tuesday','Wednesday', 
       'Thursday', 'Friday','Saturday']; 
    return addOrdinal(Math.ceil(d.getDate()/7)) + ' ' + days[d.getDay()]; 
} 

console.log(ordinalDay(new Date(2015,0,1))); // 1st Thursday 
console.log(ordinalDay(new Date(2015,0,27))); // 4th Tuesday 
console.log(ordinalDay(new Date(2015,0,31))); // 5th Saturday 
console.log(ordinalDay(new Date(2015,11,25))); // 4th Friday 
+0

원래의 문제를 설명하기 위해 약간의 말을 바꾸면, [between]은 모든 날짜를 포함하는 배열이 사용자가 지정한 범위 사이에 있다고 말할 수 있습니다. 이 배열의 첫 번째 요소는 동일한 요일을 찾기위한 기반입니다. 예를 들어 어쨌든 첫 번째 요소와 같은 주에 평일을 찾지 못하면 문제가있는 것입니다. 내 배열에서 그 평일의 첫 번째 사건을 어떻게 찾을 수 있습니까? 3 월 3 일은 1 주일의 화요일입니다. 그러나 4 월 첫 주에는 화요일이 없다.4 월 2 주 화요일에 날짜 7이 있습니다. 어떻게 거기에 도달 할 수 있습니까? – Superman

+0

범위의 첫 번째 날짜를 기준으로 매월 해당 서너 번째 요일을 원하십니까? 예 : 첫 번째 날짜가 매월 두 번째 월요일 인 경우 해당 범위의 매월 둘째 월요일을 얻으시겠습니까? – RobG

+0

예, 배열의 첫 번째 요소를 기반으로합니다. 같은 주에 같은 평일. 처음보다 먼저. 그것이 세 번째보다 세 번째 경우. 또는 첫 번째 주에 있지만 다음 달에있는 경우 첫 번째 주에 평일의 첫 번째 발생보다 평일이 없습니다. n 번째 달 마지막 주에도 동일합니다. 지난 주에 두 번째 지난 주에보기보다. 나는이 문제로 인해 너무 화가났다. :( – Superman

관련 문제