javascript 有没有办法使用年份和日期创建日期对象? ISO 周数?

发布于 2024-09-01 16:43:39 字数 157 浏览 8 评论 0原文

我正在尝试创建一个谷歌注释的时间线即。为此,我需要输入事件的日期。我所拥有的唯一信息是年份和年份。事件的 ISO 周数。 Javascript 有没有办法只使用年份和日期来创建日期对象?周数?我用谷歌搜索但没有找到任何可行的解决方案。

感谢您的帮助!

I am trying to create a google annotated timeline viz. For that I need to input the date of the event. The only information I have is the year & the ISO week number for the event. Is there a way in Javascript to create a Date object using just the year & week number? I googled it but did not come across any feasible solution.

Thanks for the help!

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

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

发布评论

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

评论(3

花开雨落又逢春i 2024-09-08 16:43:39

ISO 周从星期一开始

如果 1 月 1 日是星期一、星期二、星期三或星期四,则位于第 01 周。
如果 1 月 1 日是周五、周六或周日,则为上一年的第 52 或 53 周

Date.weekofYear= function(s){
    var yw= s.split(/\D+0?/),
    weeks= 7*yw[1]-7,
    date1= new Date(yw[0], 0, 1),
    day1= date1.getDay(),
    incr= (day1> 0 && day1<5)? -1: 1;
    if(yw[2]) weeks+= (+yw[2])-1;// optional day in week

    while(date1.getDay()!= 1){
        date1.setDate(date1.getDate()+incr);
    }
    date1.setDate(date1.getDate()+weeks);
    return date1;
}

Date.weekofYear('2010-20').toLocaleDateString()
// (use UTC methods to start the weeks on Greenwich instead of local time)
/*  returned value: (String)
Monday, May 17, 2010
*/

ISO weeks start on Monday

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01.
If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year

Date.weekofYear= function(s){
    var yw= s.split(/\D+0?/),
    weeks= 7*yw[1]-7,
    date1= new Date(yw[0], 0, 1),
    day1= date1.getDay(),
    incr= (day1> 0 && day1<5)? -1: 1;
    if(yw[2]) weeks+= (+yw[2])-1;// optional day in week

    while(date1.getDay()!= 1){
        date1.setDate(date1.getDate()+incr);
    }
    date1.setDate(date1.getDate()+weeks);
    return date1;
}

Date.weekofYear('2010-20').toLocaleDateString()
// (use UTC methods to start the weeks on Greenwich instead of local time)
/*  returned value: (String)
Monday, May 17, 2010
*/
你列表最软的妹 2024-09-08 16:43:39

有趣的问题。这是我的解决方案:

var week2date = (function() {
    var weekToDate = {
        // last week number in the month: [first date of each week]
        4:  [4, 11, 18, 25], // jan
        8:  [1, 8, 15, 22], // feb
        13: [1, 8, 15, 22, 29], // march
        17: [5, 12, 19, 26], // etc ...
        22: [3, 10, 17, 24, 31],
        26: [7, 14, 21, 28],
        30: [5, 12, 19, 26],
        35: [2, 9, 16, 23, 30],
        39: [6, 13, 20, 27],
        43: [4, 11, 18, 25],
        48: [1, 8, 15, 22, 29],
        52: [6, 13, 20, 27]
    };

    return function(week, year) {
        if ( week > 52 || week < 01 ) { return false; }

        var d = new Date(),
            lastw = 0,
            month = 0;

        for ( var w in weekToDate ) {
            if ( !weekToDate.hasOwnProperty(w) ) { continue; }
            if ( w >= week ) {
                break;
            }
            lastw = w;
            ++month;
        }

        d.setFullYear(year || d.getFullYear(), month, weekToDate[w][week - lastw - 1]);
        return d;
    }
})();

使用:

console.log(week2date(13));
console.log(week2date(38, 1983));

输出:

Mon Mar 29 2010 18:51:28 GMT-0700 (PST) {}
Tue Sep 20 1983 18:51:28 GMT-0700 (PST) {}

我使用 维基百科上的此表 来获取日期信息。

Interesting problem. Here's my solution:

var week2date = (function() {
    var weekToDate = {
        // last week number in the month: [first date of each week]
        4:  [4, 11, 18, 25], // jan
        8:  [1, 8, 15, 22], // feb
        13: [1, 8, 15, 22, 29], // march
        17: [5, 12, 19, 26], // etc ...
        22: [3, 10, 17, 24, 31],
        26: [7, 14, 21, 28],
        30: [5, 12, 19, 26],
        35: [2, 9, 16, 23, 30],
        39: [6, 13, 20, 27],
        43: [4, 11, 18, 25],
        48: [1, 8, 15, 22, 29],
        52: [6, 13, 20, 27]
    };

    return function(week, year) {
        if ( week > 52 || week < 01 ) { return false; }

        var d = new Date(),
            lastw = 0,
            month = 0;

        for ( var w in weekToDate ) {
            if ( !weekToDate.hasOwnProperty(w) ) { continue; }
            if ( w >= week ) {
                break;
            }
            lastw = w;
            ++month;
        }

        d.setFullYear(year || d.getFullYear(), month, weekToDate[w][week - lastw - 1]);
        return d;
    }
})();

Use:

console.log(week2date(13));
console.log(week2date(38, 1983));

Output:

Mon Mar 29 2010 18:51:28 GMT-0700 (PST) {}
Tue Sep 20 1983 18:51:28 GMT-0700 (PST) {}

I used this table on wikipedia to get the date information.

花开浅夏 2024-09-08 16:43:39

我赞同 @kennebec 先生的回答,但由于我还不能发表评论,我想补充一点,一年中的周表示的 ISO 表示法是 yyyy-Www。例如:2000-W01。还要注意周填充。

I second Mr.s @kennebec's answer, but since I can't yet comment, I would want to add that the ISO notation for the week of year representation is yyyy-Www. As an example: 2000-W01. Note the week padding as well.

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