How to get last friday timestamp using javascript?

I have to get the timestamp of last Friday, but I'm not sure how I can get the timestamp using a working day. Can anybody help?

What I'm trying to get is the difference between last Friday and today.

var now = new Date();
var time = now.getTime();
var fridayTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 13, 30, 00);
var timeDiff = time - fridayTime;

I know that I have not written the correct code for "fridayTime", but I'm not sure if this is the correct code.

+5
source share
5 answers

const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 7 ;
const lastFriday = new Date();
lastFriday.setDate(t);
console.log(lastFriday);
Run code
+7
source

First you need to get the diff date until last Friday:

var now = new Date(),
    day = now.getDay();

Depending on what “last Friday” means to you, use the following

  • next friday

    var diff = (day <= 5) ? (7 - 5 + day ) : (day - 5);

  • if today is Friday, then come back today, otherwise the nearest last Friday

    var diff = (7 - 5 + day) % 7;

  • var diff = 7 - 5 + day;

setDate. setDate , :

mdn:

dayValue , setDate() Date. , 0 dayValue, ​​ .

var date = new Date();
date.setDate(now.getDate() - diff);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);

var friday = date.getTime();

function getLastFridayOf(date) {
    var d = new Date(date),
        day = d.getDay(),
        diff = (day <= 5) ? (7 - 5 + day ) : (day - 5);

    d.setDate(d.getDate() - diff);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);

    return d.getTime();
}

, JS .

new Date(getLastFridayOf('2015-05-01')).toDateString() 
-> Fri Apr 24 2015

new Date(getLastFridayOf('2015-05-19')).toDateString() 
-> Fri May 15 2015

new Date(getLastFridayOf('2015-05-16')).toDateString() 
-> Fri May 15 2015

new Date(getLastFridayOf('2015-05-15')).toDateString() 
-> Fri May 08 2015
+7

, , , , . .

var target = 5, // <- Friday
    date = new Date,
    days = ( 7 - ( target - date.getDay() ) % 7 ),
    time = date.getTime() - ( days * 86400000 );

// setting full timestamp here
date.setTime(time);

// flooring the time to midnight (optional)
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);

// date should now hold last Friday     
console.log( date.toString() );

, JavaScript , ​​ . .

The same applies to time zones. If you are happy to accept such a user time, then you are probably happy that this is correct for their own time zone. Just something to be aware of.

0
source

Check it out, it seems to work, didn't check it correctly though

function getLastDayOf(dayOfWeek, date) {
    var secondsInDay = 86400;
    var startDay = date.getDay();
    var daysTillLast = -(startDay  - dayOfWeek + 7) % 7;
    return new Date(date.getTime() + secondsInDay * daysTillLast );
}

Set Version Date

function getLastDayOf(dayOfWeek, date) {
   var startDay = date.getDay();
   var daysTillLastFriday = -(startDay  - dayOfWeek + 7) % 7;
   var lastDay = new Date(date)
   lastDay.setDate(date.getDate() + daysTillLastFriday);
   return lastDay;
}

Example here

-1
source
function dates(dayOfWeek) {  // 0 for sunday, 1 for monday ...
  var date = new Date();
  date.setDate(date.getDate() + (dayOfWeek - 7 - date.getDay()) % 7);
  alert(date);
}
dates(3);  

Demo

-1
source

All Articles