0

Here's an interesting problem I'm trying to solve:

In JS, I create the following:

var millis_now = new Date().getTime();

With this information alone, I must find the time in ms (unix) of the Friday before this one at 16:00:00 local time. So for around today, July 2nd, 2014 at 15:48:00 (1404341280000) it must find the Friday from last week, June 27th, 2014 at 16:00:00 (1403910000000).

Likely that I'll need to mod it but I can't figure out exactly how to find this number. Just in case, here are some useful values that may help:

var MILLIS_PER_DAY = 86400000;
var MILLIS_PER_WEEK = 604800000;
ZekeDroid
  • 7,089
  • 5
  • 33
  • 59
  • 1
    [`Date.js`](https://code.google.com/p/datejs/) should do it. – PM 77-1 Jul 02 '14 at 22:54
  • look at that...genius. feel free to add it as the answer, it's as perfect as it gets – ZekeDroid Jul 02 '14 at 23:02
  • It's not my discovery. It was suggested in [another SO post](http://stackoverflow.com/questions/4822852/how-to-get-the-day-of-week-and-the-month-of-the-year). I had no clue how to deal with local time. – PM 77-1 Jul 02 '14 at 23:06

1 Answers1

1

last Friday = today - today's day of week - 2 days

var curtime = new Date();
var curDate = new Date(curtime.getFullYear(), curtime.getMonth(), curtime.getDate());
var lastFriday = new Date(curDate - MILLIS_PER_DAY * (curDate.getDay()+2-16/24));
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • What about "*at 16:00:00 local time*" ? – PM 77-1 Jul 02 '14 at 23:07
  • I like it but this looks to find the day from today plus 2? It was supposed to be given ANY starting date, find the previous Friday. I'm sticking to Date.js since it saves me from reinventing the wheel. Thanks though! – ZekeDroid Jul 02 '14 at 23:08
  • @ZekeDroid, oops didn't see the hour 16. See updated answer. `+2` means subtract Sunday and Saturday, and `-16/24` means adds 16 hours – Fabricator Jul 02 '14 at 23:11