How to get first and last day of the current week in JavaScript
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // 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 firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
firstday
"Sun, 06 Mar 2011 12:25:40 GMT"
lastday
"Sat, 12 Mar 2011 12:25:40 GMT"
This works for firstday = sunday of this week and last day = saturday for this week. Extending it to run Monday to sunday is trivial.
Making it work with first and last days in different months is left as an exercise for the user
JavaScript - get the first day of the week from current date
Using the getDay
method of Date objects, you can know the number of day of the week (being 0=Sunday, 1=Monday, etc).
You can then subtract that number of days plus one, for example:
function getMonday(d) {
d = new Date(d);
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
return new Date(d.setDate(diff));
}
getMonday(new Date()); // Mon Nov 08 2010
How to get first and last day of current week when days are in different months?
The getDay method returns the number of the day in the week, with Sunday as 0 and Saturday as 6. So if your week starts on Sunday, just subtract the current day number in days from the current date to get the start, and add 6 days get the end, e.g.
function getStartOfWeek(date) { // Copy date if provided, or use current date if not date = date? new Date(+date) : new Date(); date.setHours(0,0,0,0); // Set date to previous Sunday date.setDate(date.getDate() - date.getDay()); return date;}
function getEndOfWeek(date) { date = getStartOfWeek(date); date.setDate(date.getDate() + 6); return date; } document.write(getStartOfWeek());
document.write('<br>' + getEndOfWeek())
document.write('<br>' + getStartOfWeek(new Date(2016,2,27)))
document.write('<br>' + getEndOfWeek(new Date(2016,2,27)))
How can I get the first and last day of the week crossing months in javascript
You can use date-fns library for that:
const start = startOfWeek(date);
const end = endOfWeek(date);
Check out these threads for more solutions:
- How to get first and last day of current week when days are in different months?
- How to get first and last day of the week in JavaScript
How do I get the first day of the first week in a given year, in javascript?
so I have two solutions to offer
1 quite simple (making use of the Date object - basically doing what barrycarter suggested in a comment)
Creating a Date object for the first of our desired year ... and then based on which day of the week that is, adjusting the date to end up with the first monday of the first week of the year
As you might notice when running the code. The answers are off by (mostly 1 hour) - I don't know why, and so far I only encountered this when running my code on here - in VS Code and several Online JS Playgrounds the code returned exactly what I intended it to return. So yeah there's that.
(date.getDay() + 6) % 7 // 0 - 6 for Mon - Sun
function getDateOfFirstDayOfFirstWeek(year) {
const date = new Date(year, 0, 1)
if ((date.getDay() + 6) % 7 === 0) return date
if ((date.getDay() + 6) % 7 < 4) {
date.setDate(date.getDate() - (date.getDay() + 6) % 7)
return date
} else {
date.setDate(8 - (date.getDay() + 6) % 7)
return date
}
}
console.log(getDateOfFirstDayOfFirstWeek(2020))
console.log(getDateOfFirstDayOfFirstWeek(2021))
console.log(getDateOfFirstDayOfFirstWeek(2022))
console.log(getDateOfFirstDayOfFirstWeek(2100))
console.log(getDateOfFirstDayOfFirstWeek(3567))
console.log(getDateOfFirstDayOfFirstWeek(2002))
console.log(getDateOfFirstDayOfFirstWeek(1845))
Javascript : Get the first day of the week passing the current date
function getStartOfWeek(index) { var d = new Date(); var day = d.getDay(); var diff = index - day; if(index > day) { diff -= 7; } return new Date(d.setDate(d.getDate() + diff));}
document.write(getStartOfWeek(0).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(1).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(2).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(3).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(4).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(5).toLocaleDateString()+"<br/>");document.write(getStartOfWeek(6).toLocaleDateString()+"<br/>");
Related Topics
Get the Value of Checked Checkbox
Using Http Rest APIs with Angular 2
JavaScript - Cannot Set Property of Undefined
Variable Scope in D3 JavaScript
Preventdefault() on an <A> Tag
Failed to Execute 'Postmessage' on 'Domwindow': Https://Www.Youtube.Com !== Http://Localhost:9000
How to Find the Height of Text on an HTML Canvas
Capturing JavaScript Console.Log
What Is the Order of Execution in JavaScript Promises
How to Create Multidimensional Array
How to Change the Content of a <Textarea> with JavaScript
My Very Simple Greasemonkey Script Is Not Running
How to Get Selected HTML Text with JavaScript
Functions That Return a Function