How to check if date is in this week in javascript?

10,366

Solution 1

Dates are hard, I would always suggest using a library dedicated to date handling as it reduces the chances of errors in your code.

MomentJS is a good one.

var now = moment();
var input = moment("2016-04-17T11:45:00Z");
var isThisWeek = (now.isoWeek() == input.isoWeek())

Edit: Please note as of 2020 moment may not be a good choice for new projects

Solution 2

This seems to be working for me.

function isDateInThisWeek(date) {
  const todayObj = new Date();
  const todayDate = todayObj.getDate();
  const todayDay = todayObj.getDay();

  // get first date of week
  const firstDayOfWeek = new Date(todayObj.setDate(todayDate - todayDay));

  // get last date of week
  const lastDayOfWeek = new Date(firstDayOfWeek);
  lastDayOfWeek.setDate(lastDayOfWeek.getDate() + 6);

  // if date is equal or within the first and last dates of the week
  return date >= firstDayOfWeek && date <= lastDayOfWeek;
}

const date = new Date();
const isInWeek = isDateInThisWeek(date);

Solution 3

<div ng-app="myApp">
<div class="container" ng-controller="Ctrl_List">

    <h1>{{currentDate}}</h1>
    <h1>{{numberCurrentDateWeeks}}</h1>

    <h1>{{yourDate}}</h1>
    <h1>{{numberYourDateWeeks}}</h1>

 </div>
</div>

......

angular.module('myApp', [])
.controller("Ctrl_List", ["$scope", "$filter", function(s, $filter) {
  s.yourDate = '2016-04-23T11:45:00Z'
  s.currentDate = new Date();

  s.numberCurrentDateWeeks = $filter('date')(s.currentDate, "w");
  s.numberYourDateWeeks = $filter('date')(s.yourDate, "w");

}]);

then you got the Week numbers just compare or do whatever you like

cheers !

Solution 4

May not be the most optimal solution, but I think it's quite readable:

function isThisWeek (date) {
  const now = new Date();

  const weekDay = (now.getDay() + 6) % 7; // Make sure Sunday is 6, not 0
  const monthDay = now.getDate();
  const mondayThisWeek = monthDay - weekDay;

  const startOfThisWeek = new Date(+now);
  startOfThisWeek.setDate(mondayThisWeek);
  startOfThisWeek.setHours(0, 0, 0, 0);

  const startOfNextWeek = new Date(+startOfThisWeek);
  startOfNextWeek.setDate(mondayThisWeek + 7);

  return date >= startOfThisWeek && date < startOfNextWeek;
}

Solution 5

You can do that without any libraries by checking if the date.getTime() (milliseconds since epoch) is between last monday and next monday:

const WEEK_LENGTH = 604800000;

function onCurrentWeek(date) {

    var lastMonday = new Date(); // Creating new date object for today
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay()-1)); // Setting date to last monday
    lastMonday.setHours(0,0,0,0); // Setting Hour to 00:00:00:00
    


    const res = lastMonday.getTime() <= date.getTime() &&
                date.getTime() < ( lastMonday.getTime() + WEEK_LENGTH);
    return res; // true / false
}

(one week in ms = 24 * 60 * 60 * 1000 * 7 = 604,800,000)

Share:
10,366
Sophon Men
Author by

Sophon Men

Updated on June 17, 2022

Comments

  • Sophon Men
    Sophon Men almost 2 years

    I have this date "2016-04-23T11:45:00Z" and I want to check this date in this week or not ?

    Thanks,

  • xelilof
    xelilof about 8 years
    please note that your Angular version must be 1.3 and above in order for the weeks number filter to work ;)
  • xelilof
    xelilof about 8 years
    ~for example : if ( s.numberCurrentDateWeeks == s.numberYourDateWeeks ) { //bingo it's in the same current week } else if ( s.numberCurrentDateWeeks > s.numberYourDateWeeks ){ // it's in the past weeks } ~
  • Chirag
    Chirag almost 4 years
    "lastMonday.getTime() < date.getTime()" should be replaced with "lastMonday.getTime() < = date.getTime()" if date is exact Monday.
  • Forrest Wilkins
    Forrest Wilkins over 2 years
    Day.js also has an isoWeek function: day.js.org/docs/en/plugin/iso-week