13

I have to get the timestamp of last Friday, but I am not sure how can I get timestamp using weekday. Can someone please help?

What I trying to get is 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 I have not written the correct code for "fridayTime", but not sure what is the correct code.

hazzik
  • 13,019
  • 9
  • 47
  • 86
Dhirendra Kumar
  • 575
  • 1
  • 7
  • 19
  • 1
    just call Rebecca Black... sorry in all seriousness have you seen this ? http://stackoverflow.com/questions/4156434/javascript-get-the-first-day-of-the-week-from-current-date – Pogrindis May 19 '15 at 10:39
  • 2
    Check out moment.js it is a library for js date modification. – Tobi May 19 '15 at 11:03

7 Answers7

17

const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 7 ;
const lastFriday = new Date();
lastFriday.setDate(t);
console.log(lastFriday);
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Dan Jay
  • 874
  • 10
  • 27
14

First you need to get a date diff to last friday:

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

Depending on what "last friday" means to you, use following

  • nearest past friday

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

  • if today is friday, then return today, otherwise nearest past friday

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

  • friday of the last week

    var diff = 7 - 5 + day;

Then substract this from the current date, and set result to the date object using setDate function. The setDate function will correctly handle negative numbers, changing month and year respectively:

From mdn:

If the dayValue is outside of the range of date values for the month, setDate() will update the Date object accordingly. For example, if 0 is provided for dayValue, the date will be set to the last day of the previous month.

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

var friday = date.getTime();

Full code

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();
}

Following for example only, it depends on a JS engine, and may produce different results

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
hazzik
  • 13,019
  • 9
  • 47
  • 86
  • @stovroz new Date(getLastFridayOf('2015-05-16')).toDateString() -> Fri May 08 2015 – hazzik May 19 '15 at 11:43
  • Exactly. It should be Friday the 15th. Also, I would have thought the questioner would want today's date returned when run on a Friday, not last week's. – stovroz May 19 '15 at 11:49
  • 1
    @stovroz Fixed; But this is arguable, as "last friday", can mean "friday of the previous week", or "nearest friday in the past". See http://english.stackexchange.com/questions/3841/which-day-does-next-tuesday-refer-to – hazzik May 19 '15 at 12:00
0

This seems to work and ensures you still get last Friday when the current date is Friday. Also places timestamp at midnight.

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

Worth noting that any JavaScript date calculation assumes the user's computer is set to the correct date. So, don't use for anything critical.

The same point goes for timezones. If you're happy to take the user's time like this, then you're probably happy that it's right for their own timezone. Just something to be aware of.

Tim
  • 8,036
  • 2
  • 36
  • 52
  • Answers using `setDate` - This only sets the actual calendar date of the month, so may give *next* Friday depending on what the date is – Tim May 19 '15 at 10:58
  • Which part of my statement isn't true? – Tim May 19 '15 at 11:18
  • Whole. Please read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate "If the dayValue is outside of the range of date values for the month, setDate() will update the Date object accordingly. For example, if 0 is provided for dayValue, the date will be set to the last day of the previous month." – hazzik May 19 '15 at 11:24
  • My apologies. I've upvoted your answer. My answer still works though. I didn't now that about setDate – Tim May 19 '15 at 11:28
0
const t = new Date().getDate() + (6 - new Date().getDay() - 1)  - (new Date().getDay() == 6 ? 0 : 7);
const lastFriday = new Date();
lastFriday.setDate(t);
console.log(lastFriday);
Behnam
  • 1,039
  • 2
  • 14
  • 39
-1

Check this, seems to work, haven't tested it properly 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 Date version

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

Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
-1
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

ozil
  • 6,930
  • 9
  • 33
  • 56
-1
function lastFriday(){
  var date = new Date();
  date.setDate(date.getDate()-date.getDay()-2);
  return date;
}
  • 3
    Can you explain what this code is doing? Perhaps in a way that could be made more generic to find, say, last Thursday? – Thomas Upton Jun 02 '20 at 22:18
  • It's good that you attempted to help on this question, however your solution needs more fleshing out before it solves the problem at hand. Please make sure if you're providing a code solution that it does indeed answer the question, and does not just provide sample code that _might_ help them _figure it out for themselves_. – Louis Sayers Jun 03 '20 at 09:05