Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get timestamp of last Friday using JavaScript?

Tags:

javascript

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.

like image 376
Dhiru Avatar asked May 19 '15 10:05

Dhiru


People also ask

How do I get last week date in react JS?

To get last week's date, use the Date() constructor to create a new date, passing it the year, month and the day of the month - 7 to get the date of a week ago. Copied! function getLastWeeksDate() { const now = new Date(); return new Date(now. getFullYear(), now.

How do I get this week in JavaScript?

var today = new Date(); var startDay = 0; var weekStart = new Date(today. getDate() - (7 + today. getDay() - startDay) % 7); var weekEnd = new Date(today. getDate() + (7 - today.

How can I get yesterday's date from a new date?

Use the setDate() method to get the previous day of a date, e.g. date. setDate(date. getDate() - 1) .


Video Answer


1 Answers

const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 7 ;
const lastFriday = new Date();
lastFriday.setDate(t);
console.log(lastFriday);
like image 179
Dan Jay Avatar answered Oct 03 '22 02:10

Dan Jay