最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if javascript date is from a specified week ago? - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a function that can check if a given date is in a specified week ago.

For example, if the input is <1, date object>, then it asks, if the given date is from last week. If the input is <2, date object>, then it asks if the given date is from 2 weeks ago, etc.. (0 is for current week).

Week is Sun-Sat.

    this.isOnSpecifiedWeekAgo = function(weeks_ago, inputDate) {



        return false;
    };

But I don't want to use any libraries, and also I am not sure how to change the week of a date object. Does anyone know how to begin?

Thanks

I am trying to make a function that can check if a given date is in a specified week ago.

For example, if the input is <1, date object>, then it asks, if the given date is from last week. If the input is <2, date object>, then it asks if the given date is from 2 weeks ago, etc.. (0 is for current week).

Week is Sun-Sat.

    this.isOnSpecifiedWeekAgo = function(weeks_ago, inputDate) {



        return false;
    };

But I don't want to use any libraries, and also I am not sure how to change the week of a date object. Does anyone know how to begin?

Thanks

Share Improve this question edited Oct 31, 2016 at 20:51 omega asked Oct 31, 2016 at 20:49 omegaomega 44k90 gold badges286 silver badges523 bronze badges 7
  • What is your definition of a week, from sunday-saturday or withing the past 7 days? – johnny 5 Commented Oct 31, 2016 at 20:50
  • Read the docs: developer.mozilla/en-US/docs/Web/JavaScript/Reference/…. – Sumner Evans Commented Oct 31, 2016 at 20:51
  • I would prefer sun-sat. – omega Commented Oct 31, 2016 at 20:51
  • Maybe this helps: stackoverflow./questions/22991398/… – brians69 Commented Oct 31, 2016 at 20:52
  • write a function that finds the nearest sunday to a date, then just use this add days functions to calculate each week increment stackoverflow./a/3818198/1938988 – johnny 5 Commented Oct 31, 2016 at 20:55
 |  Show 2 more ments

2 Answers 2

Reset to default 7

If you want to find out a date that was a week ago, you can simply subtract 7 days from the current date:

var weekAgo = new Date();
weekAgo.setDate(weekAgo.getDate() - 7);
console.log(weekAgo.toLocaleString());

If you want to find out if a date is in a specific week, you'll need to:

  1. Work out the start date for that week
  2. Work out the end date for that week
  3. See if the date is on or after the start and on or before the end

Since your weeks are Sunday to Saturday, you can get the first day of the week from:

var weekStart = new Date();
weekStart.setDate(weekStart.getDate() - weekStart.getDay());
console.log(weekStart.toLocaleString());

The time should be zeroed, then a new date created for 7 days later. That will be midnight at the start of the following Sunday, which is identical to midnight at the end of the following Saturday. So a function might look like:

function wasWeeksAgo(weeksAgo, date) {
  // Create a date
  var weekStart = new Date();
  // Set time to 00:00:00
  weekStart.setHours(0,0,0,0);
  // Set to previous Sunday
  weekStart.setDate(weekStart.getDate() - weekStart.getDay());
  // Set to Sunday on weeksAgo
  weekStart.setDate(weekStart.getDate() - 7*weeksAgo)
  // Create date for following Saturday at 24:00:00
  var weekEnd = new Date(+weekStart);
  weekEnd.setDate(weekEnd.getDate() + 7);
  // See if date is in that week
  return date >= weekStart && date <= weekEnd;
}

 // Test if dates in week before today (1 Nov 2016)
 // 1 Oct            24 Oct
[new Date(2016,9,1), new Date(2016,9,24)].forEach(function(date) {
  console.log(date.toLocaleString() + ' ' + wasWeeksAgo(1, date));
});

Use moment.js http://momentjs./docs/#/manipulating/subtract/

We use it a lot and its a great lib.

发布评论

评论列表(0)

  1. 暂无评论