【问题标题】:JavaScript: Convert Day/Week Into YearJavaScript:将日/周转换为年
【发布时间】:2012-10-24 08:36:16
【问题描述】:

我已经使用 Stack Overflow 几个月了,但这是我的第一篇文章。

我需要一个函数来将星期数和星期几转换为 dd/mm/yyyy 格式。

我必须使用的日期值格式为day/weekNumber。例如:3/43 转换为 20XX 年 10 月 24 日星期三。年份值将是当前年份。

日期值从 1(星期一)开始。

我在网上找到了很多功能(如thisthisthis)。有些适用于 ISO 8601 日期,我认为这对我不起作用。而且我还没有找到适合我的。

提前致谢,

【问题讨论】:

    标签: javascript function date dayofweek week-number


    【解决方案1】:

    这个解决方案确实需要添加一个额外的库,但我认为这真的很值得。它是一个momentjs 库,用于处理日期和时间。它得到了积极的维护,并且有一个很棒的documentation。一旦获得 day 和 weekNumber 的值(在我们的例子中为 3 和 43),您应该执行以下操作:

    function formatInput(day, weekNumber){
    
        var currentDate = moment(new Date());     // initialize moment to a current date
        currentDate.startOf('year');              // set to Jan 1 12:00:00.000 pm this year
        currentDate.add('w',weekNumber - 1);      // add number of weeks to the beginning of the year (-1 because we are now at the 1st week)
        currentDate.day(day);                     // set the day to the specified day, Monday being 1, Sunday 7
    
        alert(currentDate.format("dddd, MMMM Do YYYY"));  // return the formatted date string 
        return currentDate.format("dddd, MMMM Do YYYY");
    }
    

    我认为这个库以后可能对您有用,并且在日期和时间操作以及格式选项方面有很多可能性。还有一个为 momentjs 编写的很棒的文档。

    【讨论】:

    • 使用“12:00:00.000 pm”是令人困惑和模棱两可的,因为对于 12 pm 是指午夜还是中午(严格来说两者都不是)没有共识。年初是 2012-01-01 00:00:00,但这不是使用 ISO 星期日期的第一周的第一天,它发生在 2012 年 1 月 2 日星期一。
    【解决方案2】:

    所以假设您分别拥有343 的值,您可以在当年的第一天做一些简单的数学运算:

    • 获取当年 1 月 1 日
    • 加 (43 * 7 + 3)

    可能是这样的:

    var currentDate = new Date();
    var startOfYear = new Date(currentDate.getFullYear(), 0, 1);//note: months start at 0
    var daysToAdd = (43 * 7) + 3;
    //add days
    startOfYear.setDate(startOfYear.getDate() + daysToAdd);
    

    Here is an example


    编辑

    再想一想,我认为我对您的要求有误。看来您需要一周中的特定日期。 Check this out 寻求更好的解决方案。

    问题在于,这完全取决于您对一周的定义。今年从星期日开始,那么这是否意味着 2012 年 2 月 1 日(今年的第一个星期一)是第二周的开始?

    我最新的例子会先查找指定周的开始,然后查找指定日的下一次出现

    【讨论】:

      【解决方案3】:

      根据 ISO 在处理星期日期时,星期从星期一开始,一年中的第一周包含一年中的第一个星期四。因此,对于 2012 年,第一周从 1 月 2 日星期一开始,2013 年的第一周将从 2012 年 12 月 31 日星期一开始。

      因此,如果 3/43 是第 43 周的第三天(即 ISO 日期 2012-W43-3),则可以使用以下方法将其转换为日期对象:

      function customWeekDateToDate(s) {
        var d, n;
        var bits = s.split('/');
      
        // Calculate Monday of first week of year this year
        d = new Date();   
        d = new Date(d.getFullYear(),0,1); // 1 jan this year
        n = d.getDay();
        d.setDate(d.getDate() + (-1 * n +( n<5? 1 : 8)));
      
        // Add days
        d.setDate(d.getDate() + --bits[0] + --bits[1] * 7);
      
        return d;
      }
      
      console.log(customWeekDateToDate('3/43')); // 01 2012-10-24
      

      请注意,这使用日期,否则夏令时转换可能会导致错误的日期。

      【讨论】:

        猜你喜欢
        • 2018-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多