I
I
Ilya Vegner2019-05-12 10:05:16
JavaScript
Ilya Vegner, 2019-05-12 10:05:16

How to display the date of the week earlier?

How can I display the date date of the week earlier without any libraries?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shohruh Shaimardonov, 2019-05-12
@joeberetta

Taken from weeknumber.net

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: http://weeknumber.net/how-to/javascript 

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}

S
svetlov97, 2019-05-12
@svetlov97

If you mean current date - 1 week then so
new Date().getTime() - 604800000;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question