给定一个日期(月/日/年),确定它是否为“第三个星期二”.月号等

Given a date (m/d/yyyy) determine if its "the third tuesday" of the month etc

本文关键字:三个 星期二 日期 一个 是否      更新时间:2023-09-26

经过几次搜索,我还没能找到我要找的东西。我使用jquery datepicker返回一个看起来像Day, Month Date, YYYY的日期字符串,我正在寻找一个库或一些方法,可以把它变成the second Tuesday of the monththe fourth Thursday of the month。到目前为止,似乎jquery的prettyDateEasyDate没有我正在寻找的功能,我想避免手工做这个!

谢谢,亚历克斯

您不需要日期库-只需将日期除以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");
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(new Date(2013, 4, 10))

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

这是我在一天中使用的脚本,如果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;
}

在Date.js中你有一些有用的函数,如moveToFirstDayOfMonthgetWeek

然后你可以得到该月第一天的星期,然后减去这两个得到该月的星期。

下面是一个简单的例子,使用date. js和离开今天,但你可以传入你自己的日期。我不太熟悉其他的Date库,但是我认为你可以在下面重新创建类似的东西。

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

我写了一些简单的逻辑,只从日期的值向后工作:

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必须为nd, strd定制