给定一个工作日和它发生的月份中的哪一天,我可以在恒定时间内获得它的序数位置吗?

发布于 2024-12-04 18:32:43 字数 712 浏览 1 评论 0原文

假设我有一个日期,我可以从中获取该月的日期和星期几。例如(在 Javascript 中):

var d = new Date(1316038581772); // Wed Sep 14 2011 12:16:21 GMT-1000
var dayOfWeek = d.getDay(); // 3 - Wednesday
var dayOfMonth = d.getDate(); // 14 - Day of month

我正在寻找一个值 n,使得该日期是该月中一周的第 n 天。在本例中,我正在寻找 2011 年 9 月的第二个星期三的 n=2

简单的算法是找到该月中该工作日的第一次出现,并计算天数差异,然后除以 7,但这不是常数时间。例如,我需要从 9 月的第一天开始迭代每一天 7 次才能到达第一个星期三。

这个问题有恒定时间的解吗?

(对于任何感兴趣的人,我正在尝试为 iCalendar 重复规则生成序数值;给定一个日期,生成每月第 n 天的每月重复值。这种情况的规则是喜欢

RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=WE;BYSETPOS=2

RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=2WE

Say I'm given a date, from which I can get its day of the month and day of the week. For example (in Javascript):

var d = new Date(1316038581772); // Wed Sep 14 2011 12:16:21 GMT-1000
var dayOfWeek = d.getDay(); // 3 - Wednesday
var dayOfMonth = d.getDate(); // 14 - Day of month

I'm looking for a value n such that this date is the nth day of the week in the month. In this case, I'm looking for n=2, for the 2nd Wednesday in September 2011.

The naive algorithm would be to find the first occurrence of that weekday in the month, take the difference in days, and divide by 7, but this isn't in constant time. For example, I would need to iterate over each day from the first day in September 7 times to reach the first Wednesday.

Is there a constant-time solution to this problem?

(For anyone interested, I'm trying to generate ordinal values for iCalendar recurrence rules; given a date, generate the monthly recurrence for the nth day of each month. The rule for this case would be something like

RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=WE;BYSETPOS=2

or

RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=2WE

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-12-11 18:32:43

除以 7 并向上舍入。

14 号始终是该月的第 2 个[工作日],15 号始终是该月的第 3 个[工作日],依此类推。

Divide by 7 and round up.

The 14th is always the 2nd [weekday] of the month, the 15th is always the 3rd [weekday] of the month, etc.

烟织青萝梦 2024-12-11 18:32:43

取该月的日期,加上 6,然后除以 7,丢弃余数。

Take the day of the month, add 6, and divide by 7, throwing away the remainder.

噩梦成真你也成魔 2024-12-11 18:32:43

您需要两个函数 - 一个用于获取当天的信息,另一个用于获取第 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(' ');
}

You need two functions- one to get the day info, the other for the nth.

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(' ');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文