Reputation: 643
I am using the following script to get Monday (first) and Sunday (last) for the previous week:
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay() - 6; // Gets day of the month (e.g. 21) - the day of the week (e.g. wednesday = 3) = Sunday (18th) - 6
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
This works fine if last Monday and Sunday were also in the same month, but I just noticed today that it doesn't work if today is December and last Monday was in November.
I'm a total JS novice, is there another way to get these dates?
Upvotes: 12
Views: 35020
Reputation: 1145
You can use a third party date library to deal with dates. For example:
var startOfWeek = moment().startOf('week').toDate();
var endOfWeek = moment().endOf('week').toDate();
if you want to use JavaScript then use the below code
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay()+1; // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
startDate = ""+startDate.getFullYear()+"-"+ (startDate.getMonth() + 1) + "-" + startDate.getDate()
var endDate = new Date(curr.setDate(last));
endDate = "" + (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();
alert(startDate+" , "+endDate)
Upvotes: 0
Reputation: 2612
This is the general solution of find any day of any week.
function getParticularDayTimestamp(lastWeekDay) {
var currentWeekMonday = new Date().getDate() - new Date().getDay() + 1;
return new Date().setDate(currentWeekMonday - lastWeekDay);
}
console.log(getParticularDayTimestamp(7)) // for last week monday
console.log(getParticularDayTimestamp(1)) // for last week sunday
console.log(getParticularDayTimestamp(14)) // for last to last week monday
console.log(getParticularDayTimestamp(8)) // for last to last week sunday
Upvotes: 2
Reputation: 11
it can be this simple.
var today = new Date();
var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay());
Upvotes: 1
Reputation: 127
Here you have a multi-purpose function:
function getThe(numOfWeeks, weekday, tense, fromDate) {
// for instance: var lastMonday = getThe(1,"Monday","before",new Date())
var targetWeekday = -1;
var dateAdjustment = clone(fromDate);
var result = clone(fromDate);
switch (weekday) {
case "Monday": targetWeekday = 8; break;
case "Tuesday": targetWeekday = 2; break;
case "Wednesday": targetWeekday = 3; break;
case "Thursday": targetWeekday = 4; break;
case "Friday": targetWeekday = 5; break;
case "Saturday": targetWeekday = 6; break;
case "Sunday": targetWeekday = 7;
}
var adjustment = 7 * (numOfWeeks - 1);
if (tense == "after") adjustment = -7 * numOfWeeks;
dateAdjustment.setDate(fromDate.getDate() - targetWeekday);
var weekday = dateAdjustment.getDay();
result.setDate(fromDate.getDate() - weekday - adjustment);
result.setHours(0,0,0,0);
return result;
}
You can find the "clone(obj)" function in the next post: https://stackoverflow.com/a/728694/6751764
Upvotes: 0
Reputation: 1159
A few answers mentioned moment, but no one wrote about this simple method:
moment().day(-13) // Monday last week
moment().day(-7) // Sunday last week
.day
sets a week day, so it doesn't matter what day is it today, only week matters.
Upvotes: 3
Reputation: 11
Using Moment you can do the following
var lastWeek = moment().isoWeek(moment().subtract(1,'w').week());
var mondayDifference = lastWeek.dayOfYear() - lastWeek.weekday() + 1;
var sundayDifference = mondayDifference - 1;
var lastMonday = moment().dayOfYear(mondayDifference);
var lastSunday = moment().dayOfYear(sundayDifference );
Upvotes: 1
Reputation: 14434
if you dont want to do it with an external library you should work with timestamps. i created a solution where you would substract 60*60*24*7*1000 (which is 604800000, which is 1 week in milliseconds) from the current Date and go from there:
var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
, day = beforeOneWeek.getDay()
, diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
, lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
, lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6));
Upvotes: 7
Reputation: 147513
You can get the previous Monday by getting the Monday of this week and subtracting 7 days. The Sunday will be one day before that, so:
var d = new Date();
// set to Monday of this week
d.setDate(d.getDate() - (d.getDay() + 6) % 7);
// set to previous Monday
d.setDate(d.getDate() - 7);
// create new date of day before
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1);
For 2012-12-03 I get:
Mon 26 Nov 2012
Sun 25 Nov 2012
Is that what you want?
// Or new date for the following Sunday
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 6);
which gives
Sun 02 Dec 2012
In general, you can manipulate date objects by add and subtracting years, months and days. The object will handle negative values automatically, e.g.
var d = new Date(2012,11,0)
Will create a date for 2012-11-30 (noting that months are zero based so 11 is December). Also:
d.setMonth(d.getMonth() - 1); // 2012-10-30
d.setDate(d.getDate() - 30); // 2012-09-30
Upvotes: 31
Reputation: 1851
You could use a library like moment.js.
See the subtract
method http://momentjs.com/docs/#/manipulating/subtract/
Upvotes: 4