2013-05-11 50 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

相关问题