从整个一周开始(Get whole week from date)

编程入门 行业动态 更新时间:2024-10-27 22:28:12
从整个一周开始(Get whole week from date)

我试图找到基于单个日期形成一周的所有日期。

因此,如果日期位于星期三的中间,那么它将在周日,周一,直到周六返回。

这是一个例子:

所以,假设我有2012年10月10日星期三

我会将这个日期对象传递给函数,函数将返回['2012-10-07', '2012-10-08', '2012-10-09', '2012-10-10', '2012-10-11', '2012-10-12', '2012-10-13'] (我不想要字符串,但Date对象,字符串仅用于示例^^

我已经尝试过自己了,但是我创建的方法很长很愚蠢,无论如何都是这样: https : //gist.github.com/3889121

但我在这里寻找的主要是指示。 使用什么,有更简单的方法吗?

谢谢!

I'm trying to find all the dates that would form a whole week based on a single date.

So if the date is located in the middle of the week like a Wednesday, it would return Sunday, Monday, up to Saturday.

Here's an example:

So let's say I have Wed 10 Oct 2012.

I would pass this date object to the function and the function would return ['2012-10-07', '2012-10-08', '2012-10-09', '2012-10-10', '2012-10-11', '2012-10-12', '2012-10-13'] (I don't want strings, but Date objects, strings are there just for the example ^^

I already tried myself, but the method I created is very long and dumb, here it is anyways: https://gist.github.com/3889121

But what I'm primarly looking for here is instructions. What to use, is there an easier way?

Thanks!

最满意答案

这是一个函数,它返回从给定日期之前的第一个星期日到下一个星期日的Date实例数组:

function getWeek(fromDate){ var sunday = new Date(fromDate.setDate(fromDate.getDate()-fromDate.getDay())) ,result = [new Date(sunday)]; while (sunday.setDate(sunday.getDate()+1) && sunday.getDay()!==0) { result.push(new Date(sunday)); } return result; } // usage var week = getWeek(new Date('2012/10/10')); console.log(week[0]); //=> Sun Oct 07 2012 00:00:00 console.log(week[6]); //=> Sat Oct 13 2012 00:00:00

只是为了好玩,使用Array.map的较短版本(oneliner)作为Array.map的扩展

Date.prototype.getWeek = function(){ return [new Date(this.setDate(this.getDate()-this.getDay()))] .concat( String(Array(6)).split(',') .map ( function(){ return new Date(this.setDate(this.getDate()+1)); }, this ) ); } // usage new Date('2012/10/10').getWeek(); //=> [07/10/2012, ... ,13/10/2012]

Here's a function that returns an Array of Date instances from the first sunday before the given date until the next sunday:

function getWeek(fromDate){ var sunday = new Date(fromDate.setDate(fromDate.getDate()-fromDate.getDay())) ,result = [new Date(sunday)]; while (sunday.setDate(sunday.getDate()+1) && sunday.getDay()!==0) { result.push(new Date(sunday)); } return result; } // usage var week = getWeek(new Date('2012/10/10')); console.log(week[0]); //=> Sun Oct 07 2012 00:00:00 console.log(week[6]); //=> Sat Oct 13 2012 00:00:00

Just for fun, a shorter version (a oneliner) using Array.map, as an extension of the Date.prototype

Date.prototype.getWeek = function(){ return [new Date(this.setDate(this.getDate()-this.getDay()))] .concat( String(Array(6)).split(',') .map ( function(){ return new Date(this.setDate(this.getDate()+1)); }, this ) ); } // usage new Date('2012/10/10').getWeek(); //=> [07/10/2012, ... ,13/10/2012]

更多推荐

本文发布于:2023-08-06 20:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:week   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!