• <tfoot id='cR3OI'></tfoot>
      <bdo id='cR3OI'></bdo><ul id='cR3OI'></ul>

    <i id='cR3OI'><tr id='cR3OI'><dt id='cR3OI'><q id='cR3OI'><span id='cR3OI'><b id='cR3OI'><form id='cR3OI'><ins id='cR3OI'></ins><ul id='cR3OI'></ul><sub id='cR3OI'></sub></form><legend id='cR3OI'></legend><bdo id='cR3OI'><pre id='cR3OI'><center id='cR3OI'></center></pre></bdo></b><th id='cR3OI'></th></span></q></dt></tr></i><div id='cR3OI'><tfoot id='cR3OI'></tfoot><dl id='cR3OI'><fieldset id='cR3OI'></fieldset></dl></div>
    <legend id='cR3OI'><style id='cR3OI'><dir id='cR3OI'><q id='cR3OI'></q></dir></style></legend>
      1. <small id='cR3OI'></small><noframes id='cR3OI'>

        如何在Javascript中将周数转换为日期

        时间:2023-09-05

            <tfoot id='B0bvo'></tfoot>
              <bdo id='B0bvo'></bdo><ul id='B0bvo'></ul>
                <tbody id='B0bvo'></tbody>

              <i id='B0bvo'><tr id='B0bvo'><dt id='B0bvo'><q id='B0bvo'><span id='B0bvo'><b id='B0bvo'><form id='B0bvo'><ins id='B0bvo'></ins><ul id='B0bvo'></ul><sub id='B0bvo'></sub></form><legend id='B0bvo'></legend><bdo id='B0bvo'><pre id='B0bvo'><center id='B0bvo'></center></pre></bdo></b><th id='B0bvo'></th></span></q></dt></tr></i><div id='B0bvo'><tfoot id='B0bvo'></tfoot><dl id='B0bvo'><fieldset id='B0bvo'></fieldset></dl></div>

                  <legend id='B0bvo'><style id='B0bvo'><dir id='B0bvo'><q id='B0bvo'></q></dir></style></legend>

                  <small id='B0bvo'></small><noframes id='B0bvo'>

                • 本文介绍了如何在Javascript中将周数转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我曾试图找到一些已完成的工作,但我没有运气.有什么想法吗?

                  I had tried to find some work done but I haven't had luck. Any ideas?

                  例子:

                  2001 年 1 月 1 日 => 2001-01-01

                  Week, 1, 2001 => 2001-01-01

                  2007 年 26 月周 => 2007-06-01

                  Week, 26, 2007 => 2007-06-01

                  推荐答案

                  由于 Kevin 的代码没有正确实现 ISO 8601(一年的第一周的第一天必须是星期一),我已经更正它并最终得到(也 在 jsfiddle 上查看):

                  As Kevin's code does not implement ISO 8601 properly (first day of the first week of year must be a Monday), I've corrected it and ended up with (also check it on jsfiddle):

                  function firstDayOfWeek(week, year) { 
                  
                      if (year==null) {
                          year = (new Date()).getFullYear();
                      }
                  
                      var date       = firstWeekOfYear(year),
                          weekTime   = weeksToMilliseconds(week),
                          targetTime = date.getTime() + weekTime;
                  
                      return date.setTime(targetTime); 
                  
                  }
                  
                  function weeksToMilliseconds(weeks) {
                      return 1000 * 60 * 60 * 24 * 7 * (weeks - 1);
                  }
                  
                  function firstWeekOfYear(year) {
                      var date = new Date();
                      date = firstDayOfYear(date,year);
                      date = firstWeekday(date);
                      return date;
                  }
                  
                  function firstDayOfYear(date, year) {
                      date.setYear(year);
                      date.setDate(1);
                      date.setMonth(0);
                      date.setHours(0);
                      date.setMinutes(0);
                      date.setSeconds(0);
                      date.setMilliseconds(0);
                      return date;
                  }
                  
                  /**
                   * Sets the given date as the first day of week of the first week of year.
                   */
                  function firstWeekday(firstOfJanuaryDate) {
                      // 0 correspond au dimanche et 6 correspond au samedi.
                      var FIRST_DAY_OF_WEEK = 1; // Monday, according to iso8601
                      var WEEK_LENGTH = 7; // 7 days per week
                      var day = firstOfJanuaryDate.getDay();
                      day = (day === 0) ? 7 : day; // make the days monday-sunday equals to 1-7 instead of 0-6
                      var dayOffset=-day+FIRST_DAY_OF_WEEK; // dayOffset will correct the date in order to get a Monday
                      if (WEEK_LENGTH-day+1<4) {
                          // the current week has not the minimum 4 days required by iso 8601 => add one week
                          dayOffset += WEEK_LENGTH;
                      }
                      return new Date(firstOfJanuaryDate.getTime()+dayOffset*24*60*60*1000);
                  }
                  
                  function assertDateEquals(effectiveDate, expectedDate, description) {
                      if ((effectiveDate==null ^ expectedDate==null) || effectiveDate.getTime()!=expectedDate.getTime()) {
                          console.log("assert failed: "+description+"; effective="+effectiveDate+", expected="+expectedDate);
                      }
                  }
                  function assertEquals(effectiveValue, expectedValue, description) {
                      if (effectiveValue!=expectedValue) {
                          console.log("assert failed: "+description+"; effective="+effectiveValue+", expected="+expectedValue);
                      }
                  }
                  
                  // expect the first day of year to be a monday
                  for (var i=1970; i<2050; i++) {
                      assertEquals(firstWeekOfYear(i).getDay(), 1, "first day of year "+i+" must be a monday"); // 1=Monday
                  }
                  
                  // assert some future first day of first week of year; source: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php
                  assertDateEquals(firstWeekOfYear(2013), new Date(Date.parse("Dec 31, 2012")), "2013");
                  assertDateEquals(firstWeekOfYear(2014), new Date(Date.parse("Dec 30, 2013")), "2014");
                  assertDateEquals(firstWeekOfYear(2015), new Date(Date.parse("Dec 29, 2014")), "2015");
                  assertDateEquals(firstWeekOfYear(2016), new Date(Date.parse("Jan 4, 2016")), "2016");
                  assertDateEquals(firstWeekOfYear(2017), new Date(Date.parse("Jan 2, 2017")), "2017");
                  assertDateEquals(firstWeekOfYear(2018), new Date(Date.parse("Jan 1, 2018")), "2018");
                  assertDateEquals(firstWeekOfYear(2019), new Date(Date.parse("Dec 31, 2018")), "2019");
                  assertDateEquals(firstWeekOfYear(2020), new Date(Date.parse("Dec 30, 2019")), "2020");
                  assertDateEquals(firstWeekOfYear(2021), new Date(Date.parse("Jan 4, 2021")), "2021");
                  assertDateEquals(firstWeekOfYear(2022), new Date(Date.parse("Jan 3, 2022")), "2022");
                  assertDateEquals(firstWeekOfYear(2023), new Date(Date.parse("Jan 2, 2023")), "2023");
                  assertDateEquals(firstWeekOfYear(2024), new Date(Date.parse("Jan 1, 2024")), "2024");
                  assertDateEquals(firstWeekOfYear(2025), new Date(Date.parse("Dec 30, 2024")), "2025");
                  assertDateEquals(firstWeekOfYear(2026), new Date(Date.parse("Dec 29, 2025")), "2026");
                  
                  console.log("All assertions done.");
                  

                  我包含了一些日期的测试用例,以检查一年中第一周的第一天是星期一,并根据 http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php

                  I included test cases for some dates to check that the first day of the first week of year is a Monday and checked some dates based on http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php

                  这篇关于如何在Javascript中将周数转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Angular 中的货币格式化指令 下一篇:时刻js中的自定义长日期格式

                  相关文章

                  最新文章

                  <i id='n0dWa'><tr id='n0dWa'><dt id='n0dWa'><q id='n0dWa'><span id='n0dWa'><b id='n0dWa'><form id='n0dWa'><ins id='n0dWa'></ins><ul id='n0dWa'></ul><sub id='n0dWa'></sub></form><legend id='n0dWa'></legend><bdo id='n0dWa'><pre id='n0dWa'><center id='n0dWa'></center></pre></bdo></b><th id='n0dWa'></th></span></q></dt></tr></i><div id='n0dWa'><tfoot id='n0dWa'></tfoot><dl id='n0dWa'><fieldset id='n0dWa'></fieldset></dl></div>
                  • <bdo id='n0dWa'></bdo><ul id='n0dWa'></ul>

                  1. <legend id='n0dWa'><style id='n0dWa'><dir id='n0dWa'><q id='n0dWa'></q></dir></style></legend><tfoot id='n0dWa'></tfoot>

                    <small id='n0dWa'></small><noframes id='n0dWa'>