johndavedecano
johndavedecano

Reputation: 522

How to get the first and last date of the previous week?

I am trying to get the last week's first and last date. My code gets the current week's first and last date.

var curr = new Date;
var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
var lastday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6));

Upvotes: 0

Views: 620

Answers (1)

vinod kumar
vinod kumar

Reputation: 348

What ever the day you run the program it will just give the last week start and end dates.

 var date= new Date();
 var dummy =  date.getDay();
 dummy = dummy + 7;
 date.setDate(date.getDate() - dummy );
 alert("previous week first day : "+ date);
 date.setDate(date.getDate() + 6);
 alert("previous week lastday : "+ date);

Best of Luck.

Upvotes: 3

Related Questions