如何使用 javascript 找到某一天属于星期几?

发布于 2024-08-09 01:50:06 字数 133 浏览 6 评论 0原文

我想在 javascript 中找到一天属于星期几。我对周的定义是截至该日期为止的星期六数。所以一月份的前几天算上一年的最后一周。从那里开始,第一个星期六到前一个星期五是第 0 周(基于 0 的周计数)。

有人知道如何实现这样的功能吗?

I want to find the number of the week which a day belongs to in javascript. My definition of week is the number of saturdays there have been up to that date. So the first few days of january are counted as the final week of the previous year. From there, the first saturday to the preceding friday is week 0 (0 based week counting).

Anyone know how to implement a function like this?

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

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

发布评论

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

评论(4

伴随着你 2024-08-16 01:50:06

这?

function week(date) {
    var firstSat = new Date(date.getFullYear(), 0, 1);
    firstSat.setDate(firstSat.getDate() + (6 - firstSat.getDay()));

    var delta = Math.floor((date - firstSat) / (7*24*60*60*1000));

    return delta < 0 ?
           delta + 52 : // first few days before 1st Sat
           delta
}

week(new Date(2009,0,1)); // Jan 1, 2009 => 51 => "1 week into last year"
week(new Date(2009,0,2)); // 51
week(new Date(2009,0,3)); // 0 => "[beginning of] week 1"
week(new Date(2009,0,10)); // 1 => "[beginning of] week 2"
week(new Date(2009,0,11)); // 1 => "still week 2"
week(new Date(2009, 9, 30)); // Fri Oct 30, 2009 => 42

This?

function week(date) {
    var firstSat = new Date(date.getFullYear(), 0, 1);
    firstSat.setDate(firstSat.getDate() + (6 - firstSat.getDay()));

    var delta = Math.floor((date - firstSat) / (7*24*60*60*1000));

    return delta < 0 ?
           delta + 52 : // first few days before 1st Sat
           delta
}

week(new Date(2009,0,1)); // Jan 1, 2009 => 51 => "1 week into last year"
week(new Date(2009,0,2)); // 51
week(new Date(2009,0,3)); // 0 => "[beginning of] week 1"
week(new Date(2009,0,10)); // 1 => "[beginning of] week 2"
week(new Date(2009,0,11)); // 1 => "still week 2"
week(new Date(2009, 9, 30)); // Fri Oct 30, 2009 => 42
纸短情长 2024-08-16 01:50:06
var getWeekForDate = function(date) {
  var yearStart = new Date(date.getFullYear(), 0, 0);

  // move to first Saturday
  yearStart.setDate(yearStart.getDate() + (6 - yearStart.getDay()));

  var msSinceStart = date - yearStart;
  var daysSinceStart = msSinceStart / (1000 * 60 * 60 * 24);
  var weeksSinceStart = daysSinceStart / 7;
  return Math.floor(weeksSinceStart);
};
var getWeekForDate = function(date) {
  var yearStart = new Date(date.getFullYear(), 0, 0);

  // move to first Saturday
  yearStart.setDate(yearStart.getDate() + (6 - yearStart.getDay()));

  var msSinceStart = date - yearStart;
  var daysSinceStart = msSinceStart / (1000 * 60 * 60 * 24);
  var weeksSinceStart = daysSinceStart / 7;
  return Math.floor(weeksSinceStart);
};
烟─花易冷 2024-08-16 01:50:06

最后,我选择了:

Date.prototype.getWeek = function()
{
 var localOffset = this.getTimezoneOffset() * 60000;
 var date = new Date(this-localOffset);
 var firstSat = new Date(this);
 firstSat.setDate(1);
 firstSat.setMonth(0);
 firstSat.setDate(firstSat.getDate() + (6 - firstSat.getDay()));

 if(date < firstSat)
 {
  date.setDate(date.getDate()-7);
  return date.getWeek()+1;
 }
 else
 {
  return Math.floor((date - firstSat) / (7*24*60*60*1000));
 }
}

这消除了 UTC 偏移量的问题

In the end, I went for:

Date.prototype.getWeek = function()
{
 var localOffset = this.getTimezoneOffset() * 60000;
 var date = new Date(this-localOffset);
 var firstSat = new Date(this);
 firstSat.setDate(1);
 firstSat.setMonth(0);
 firstSat.setDate(firstSat.getDate() + (6 - firstSat.getDay()));

 if(date < firstSat)
 {
  date.setDate(date.getDate()-7);
  return date.getWeek()+1;
 }
 else
 {
  return Math.floor((date - firstSat) / (7*24*60*60*1000));
 }
}

Which removes problems with UTC offsets

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