Getting last and this monday as Unix timestamp in JavaScript
I would suggest you use a date library like moment.js for this. That way, you don't have to do this all over again. With that library, it's just a few subtraction calls.

&lt;script type="text/javascript"&gt;


By default, the browser executes JS. type is not required. The only time type is every used is if you want to tell the browser that it contains something not JS, preventing it from executing as JS.

24*60*60*1000

60 * 60 * 24 * 7 * 1000


These are constants. I suggest you use const with the convention of uppercase and underscores. If you can't use const, var will have to do but the naming convention stays.

new Date().getTime()


Use Date.now(). It's the same as new Date().getTime() without the Date object that it generates. Also, your code uses a lot of Date objects when the operation can be done with millisecond manipulation. Try avoiding the creation of objects when necessary.

setHours mutates the date object hours using local time. This means the hours you set are midnight local time. Unless this is something you intended or something UI only, this may cause issues with timekeeping.

In some regions, weeks start on a Sunday. "This Monday" may refer to the Monday that comes after that Sunday. "Last Monday" may also refer to this week's Monday that already passed. This makes the phrase "This Monday" and "Last Monday" vague.

Anyways, here's my take on it. Not much of a departure, but there's some notable changes.



(function() {
  'use strict'

  const WEEK_IN_MS = 604800000;
  const DAY_IN_MS = 86400000;
  const DAY_MONDAY = 1;

  function getDateThisMonday() {

    // start with today's midnight
    let today = new Date();
    today.setHours(0, 0, 0);

    // We start the week with Monday, we assign 7 when its a Sunday (0)
    let dayToday = today.getDay() || 7;

    // Calculate milliseconds elapsed since Monday
    let msSinceMonday = ((dayToday - DAY_MONDAY) * DAY_IN_MS);

    // Subtract today midnight with the diff to get midnight of the Monday
    let mondayTime = today.getTime() - msSinceMonday;

    return mondayTime;
  }

  // This is pretty much the same except we subtract an additional week.
  function getDateLastMonday() {
    return getDateThisMonday() - WEEK_IN_MS;
  }

  document.write('Last Monday: ' + new Date(getDateLastMonday()));
  document.write('This Monday: ' + new Date(getDateThisMonday()));

}());