2

I'm having the following formula:

=TODAY()-WEEKDAY(TODAY(),3)-84

and this gives me Monday, 12 weeks ago.

I'm working on this code, but it gives me only a certain days back

var twelweWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var startDate = Utilities.formatDate(
  oneWeekAgo, 
  Session.getScriptTimeZone(),
  'yyyy-MM-dd'
);
1
1

This piece of code does the same.

Code

function getPastweeks(weeks) {
  var cDate = new Date();
  return new Date(cDate.getTime() - getMs(weeks) - getCorr(cDate.getDay()));
}

function getCorr(day) {
  return day >= 1 ? getMs(day - 1) / 7 : getMs(6) / 7;
}

function getMs(value) {
  return value * 7 * 24 * 60 * 60 * 1000;
}

Screenshot

enter image description here

Example

I've created an example file for you: How do I get a weekday 12 weeks ago

4
  • Hi Jacob, thanks for your feedback. I've just sent a request to access the file. – Thomas Kristiansen Feb 24 '15 at 12:38
  • @ThomasKristiansen Was it of use? – Jacob Jan Tuinstra Feb 24 '15 at 12:38
  • @ThomasKristiansen Example file is accessible. – Jacob Jan Tuinstra Feb 24 '15 at 12:40
  • Yeah, that are perfect! Thanks! – Thomas Kristiansen Feb 24 '15 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.