2013-05-11 24 views
5

經過多次搜索,我一直無法找到我要找的東西。即時通訊使用jquery datepicker返回一個日期字符串,看起來像Day, Month Date, YYYY,我正在尋找一個庫或某種方法,將採取並將其變爲 the second Tuesday of the monththe fourth Thursday of the month。到目前爲止,它似乎像jquery的prettyDateEasyDate沒有我正在尋找的功能,我很樂意避免這樣做的手!給定一個日期(m/d/yyyy)來確定它的月份「第三個星期二」等。

謝謝, 亞歷克斯

+3

我只用它對於基本的東西,所以我不確定它是否能夠解決您的所有需求,但您可以查看:http://momentjs.com/ – 2013-05-11 01:16:32

+0

看起來不像它具有「nth mon/tues/etc」的功能本月「 – 2013-05-11 01:22:41

+0

看到這個問題:http://stackoverflow.com/a/16467099/592253 – Xotic750 2013-05-11 01:34:36

回答

0

這是我在天回用的腳本,如果momentjs不適合你的工作(雖然我真的覺得momentjs是一個更好的解決方案)。

/* 
Parameters: 
index: n'th occurrence of the specified day 
day: daynumber - javascript way where sunday is 0 and is saturday is 6 
month: javascript way which is 0-11 [optional - defaults to current] 
year: Full year - four digits [optional - defaults to current] 
*/ 
function getNthDayOfMonth(index,day,month,year){ 
// Create date object 
var date = new Date(); 
// Set to first day of month 
date.setDate(1); 
// If supplied - set the month 
if(month!==''&&month!==undefined){ 
    // Set month 
    date.setMonth(month); 
}else{ 
    month = date.getMonth(); 
} 
// If supplied - set the year 
if(year!==''&&year!==undefined){ 
    // Set year 
    date.setFullYear(year); 
}else{ 
    year = date.getFullYear(); 
} 
// Find daynumber 
firstDay = date.getDay(); 
// Find first friday. 
while(date.getDay()!=day){  
    date.setDate(date.getDate()+1) ; 
} 
switch(index){ 
    case 2: 
     date.setDate(date.getDate()+7);   
    break; 
    case 3: 
     date.setDate(date.getDate()+14);     
    break; 
    case 4: 
     date.setDate(date.getDate()+21); 
    break; 
    case 5: 
     date.setDate(date.getDate()+28); 
     if(date.getMonth()!==month){ 
      date = null; 
     } 
    break; 
} 
return date; 
} 
0

Date.js你有一個像moveToFirstDayOfMonthgetWeek一些有用的功能。

然後,您可以獲取每月第一天的一週,並扣除這兩個月以獲取該月的一週。

下面是一個簡單的例子,使用Date.js,今天就離開,但你可以傳入你自己的日期。我對其他日期庫不太熟悉,但我認爲您可以重新創建下面的內容。

function dayOfWeekToday(){ 
    //Get the total weeks on the year from this day 
    var totalWeek = Date.today().getWeek(); 

    //Get the first day of this month 
    var firstOfMonth = Date.today().moveToFirstDayOfMonth(); 

    //Get the total weeks form the first day of the month 
    var weeksAtStartOfMonth = firstOfMonth.getWeek(); 
    //Get the week of the month 
    var week = totalWeek - weeksAtStartOfMonth; 

    //Get the day(0-6) of today and the first day 
    var today = Date.today().getDay(); 
    var firstDay = firstOfMonth.getDay(); 

    //If today is before the firstDay then the week will be off by one 
    if(today < firstDay){ 
     week--; 
    } 
    //Get the day form of the string 
    var day = Date.today().toString("dddd"); 

    //Translate from a number to a string 
    var str = "first"; 
    if(week === 1){ 
      str = "second"; 
    }else if(week === 2){ 
     str = "third"; 
    }else if(week === 3){ 
     str = "fourth"; 
    }else if (week === 4){ 
     str = "fifth"; 
    } 
    //Result 
    return "The "+str+" "+day+" of the month"; 
} 
0

我寫了一些簡單的邏輯,只是倒過來從日期的值:

function calculateNthDate(startDateDay, startDateValue){ 
    var day = startDateDay; 
    var date = startDateValue; 
    var i = 0; 

    while(date >= 0){ 

     if (date > 0){ 
      i++; 
     } 
     date = date - 7; 
    } 
    var string = "The "+i+'th '+day+" of the month."; 
} 

編輯:th將不得不進行自定義ndstrd

7

你不需要一個日期庫 - 只取日期,除以7並四捨五入。

//format: Day, Month Date, YYYY 
var ordinals = ["", "first", "second", "third", "fourth", "fifth"]; 
var date = "Friday, May 10, 2013"; 
var tokens = date.split(/[ ,]/); 
// tokens = ["Friday", "", "May", "10", "", "2013"]; 
console.log("The " + ordinals[Math.ceil(tokens[3]/7)] + " " + tokens[0] + " of the month"); 
0
function nthDay(D){ 
    var nth= ['First', 'Second', 'Third', 'Fourth', 'Fifth'], 
    dayNames= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 
    'Thursday', 'Friday', 'Saturday'], 
    monthNames= ['January', 'February', 'March', 'April', 'May', 'June', 
    'July', 'August', 'September', 'October', 'November', 'December']; 

    return nth[Math.floor(D.getDate()/7)]+' '+ 
    dayNames[D.getDay()]+' of '+monthNames[D.getMonth()]; 
} 

nthDay(新日期(2013年,4,10))

/*返回值:五月 的 第二個星期五*/

+0

這失敗了。 http://jsfiddle.net/7pd3vebc/這是9月的第一個星期一。使用ceil並將第一個數組成員留空 - > http://jsfiddle.net/xka6hab9/ – ssaltman 2015-03-11 16:59:35

相關問題