7

经过几次搜索,我一直无法找到我要找的东西。我jquery datepicker用来返回一个看起来像的日期字符串,Day, Month Date, YYYY我正在寻找一个库或一些方法来将它转换为 the second Tuesday of the month, 或the fourth Thursday of the month. 到目前为止,它看起来像 jqueryprettyDate并且EasyDate没有我正在寻找的功能,我很想避免手动执行此操作!

谢谢,亚历克斯

4

5 回答 5

11

您不需要日期库 - 只需获取日期,除以 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");
于 2013-05-11T02:31:08.467 回答
1
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))

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

于 2013-05-11T02:39:18.680 回答
0

In Date.js you have some helpful functions like moveToFirstDayOfMonth and getWeek.

Then you can get the week of the first day of the month and subtract those two to get the week of the month.

Here is a simple example using Date.js and going off of today but you can pass in your own date. I am not too familiar with other Date libraries but I think you can recreate something like this below.

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";
}
于 2013-05-11T01:55:46.000 回答
0

I wrote some simple logic that just works backwards from the value of the date:

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.";   
}

edit: the th will have to be customized for nd, st, and rd .

于 2013-05-11T01:57:17.023 回答
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;
}
于 2013-05-11T01:30:36.043 回答