Search code examples
javascriptjqueryepoch

Find range of times in ms given current time


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;

Solution

  • 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));