Javascript 给定一个工作日和它发生的月份的哪一天,我能得到它在恒定时间内的顺序位置吗?
假设给我一个日期,从中我可以得到它的月份和星期几。例如(在Javascript中): 我正在寻找一个值Javascript 给定一个工作日和它发生的月份的哪一天,我能得到它在恒定时间内的顺序位置吗?,javascript,algorithm,date,calendar,icalendar,Javascript,Algorithm,Date,Calendar,Icalendar,假设给我一个日期,从中我可以得到它的月份和星期几。例如(在Javascript中): 我正在寻找一个值n,这样这个日期就是这个月一周中的第几天。在本例中,我正在寻找2011年9月第二个星期三的n=2 最简单的算法是找到一个月中第一个工作日,以天为单位,除以7,但这不是固定时间。例如,从9月的第一天开始,我需要每天迭代7次才能到达第一个星期三 这个问题有固定时间的解决方案吗 (对于任何感兴趣的人,我正在尝试为iCalendar定期规则生成序数值;给定一个日期,为每个月的n第几天生成每月定期。这种情
n
,这样这个日期就是这个月一周中的第几天。在本例中,我正在寻找2011年9月第二个星期三的n=2
最简单的算法是找到一个月中第一个工作日,以天为单位,除以7,但这不是固定时间。例如,从9月的第一天开始,我需要每天迭代7次才能到达第一个星期三
这个问题有固定时间的解决方案吗
(对于任何感兴趣的人,我正在尝试为iCalendar定期规则生成序数值;给定一个日期,为每个月的n
第几天生成每月定期。这种情况下的规则类似于
RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=WE;BYSETPOS=2
或
)取一个月的某一天,加上6,除以7,扔掉剩余的部分。取一个月的某一天,加上6,除以7,扔掉剩余部分。除以7,四舍五入
14号总是一个月的第二个[工作日],15号总是一个月的第三个[工作日],以此类推。除以7并取整
14号总是一个月的第二个[工作日],15号总是一个月的第三个[工作日],等等。您需要两个功能-一个用于获取日期信息,另一个用于第n个
Number.prototype.nth= function(){
var n= Math.round(this), t= Math.abs(n%100), i= t%10;
if(i<4 && (t<4 || t> 20)){
switch(i){
case 1:return n+'st';
case 2:return n+'nd';
case 3:return n+'rd';
}
}
return n+'th';
}
Date.prototype.nthofMonth= function(){
var today= this.getDate(),m=this.getMonth(),
day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'][this.getDay()],
month= ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'][m];
return [(m+1)+'-'+today,'the ', (Math.ceil((today)/7)).nth(), day, 'of', month, 'in', this.getFullYear()].join(' ');
}
Number.prototype.nth=function(){
var n=Math.round(this),t=Math.abs(n%100),i=t%10;
如果(i您需要两个函数-一个用于获取日期信息,另一个用于第n个
Number.prototype.nth= function(){
var n= Math.round(this), t= Math.abs(n%100), i= t%10;
if(i<4 && (t<4 || t> 20)){
switch(i){
case 1:return n+'st';
case 2:return n+'nd';
case 3:return n+'rd';
}
}
return n+'th';
}
Date.prototype.nthofMonth= function(){
var today= this.getDate(),m=this.getMonth(),
day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'][this.getDay()],
month= ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'][m];
return [(m+1)+'-'+today,'the ', (Math.ceil((today)/7)).nth(), day, 'of', month, 'in', this.getFullYear()].join(' ');
}
Number.prototype.nth=function(){
var n=Math.round(this),t=Math.abs(n%100),i=t%10;
如果(这是一个很好的方式来获取我想要的英语短语,但我实际上只是在寻找Math.ceil((今天)/7)
部分。如果我今天没有放屁,我可能会自己发现它。这是一个很好的方式来获取我想要的英语短语,但我实际上只是在寻找Math.ceil((今天)/7)
部分。如果我今天没有放屁,我可能会自己发现它。
Number.prototype.nth= function(){
var n= Math.round(this), t= Math.abs(n%100), i= t%10;
if(i<4 && (t<4 || t> 20)){
switch(i){
case 1:return n+'st';
case 2:return n+'nd';
case 3:return n+'rd';
}
}
return n+'th';
}
Date.prototype.nthofMonth= function(){
var today= this.getDate(),m=this.getMonth(),
day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'][this.getDay()],
month= ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'][m];
return [(m+1)+'-'+today,'the ', (Math.ceil((today)/7)).nth(), day, 'of', month, 'in', this.getFullYear()].join(' ');
}