给定一个日期(月/日/年),确定它是否为“第三个星期二”.月号等
Given a date (m/d/yyyy) determine if its "the third tuesday" of the month etc
经过几次搜索,我还没能找到我要找的东西。我使用jquery datepicker
返回一个看起来像Day, Month Date, YYYY
的日期字符串,我正在寻找一个库或一些方法,可以把它变成the second Tuesday of the month
或the fourth Thursday of the month
。到目前为止,似乎jquery的prettyDate
和EasyDate
没有我正在寻找的功能,我想避免手工做这个!
谢谢,亚历克斯
您不需要日期库-只需将日期除以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中你有一些有用的函数,如moveToFirstDayOfMonth
和getWeek
。
然后你可以得到该月第一天的星期,然后减去这两个得到该月的星期。
下面是一个简单的例子,使用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
, st
和rd
定制
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何使用PHP和JS级联三个下拉列表
- 灯光问题(使用三个.js)
- 在三个.js中创建球体(例如地球)的磁力线
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 根据前两个下拉ID显示第三个下拉列表
- 在数组JS中查找三个重复项
- Meteor和S3:如何通过一次上传/编辑定义三个不同的图像
- 如何替换javascript字符串中的前三个连字符
- 保存两个模型(属于第三个模型)和一个提交
- 将文本字段限制为三个数字
- 三个js键盘旋转
- 匹配三个正则表达式中的任意一个,以先到的为准(在javascript中)
- 加载模型与三个.js
- 连续三个检查宾果游戏
- 我需要帮助查找我的JavaScript小游戏中的错误(三个我没有得到的错误)
- 如何在导入的 Collada 模型上使用 EdgesHelper 在三个.js.
- 给定一个日期(月/日/年),确定它是否为“第三个星期二”.月号等