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
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
javascript get first and last day of the previous week
You have to clone beforeOneWeek
. Everything else looks fin in your code
var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
var beforeOneWeek2 = new Date(beforeOneWeek);
day = beforeOneWeek.getDay()
diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
lastSunday = new Date(beforeOneWeek2.setDate(diffToMonday + 6));
$( "#dateDebut" ).val($.datepicker.formatDate('dd/mm/yy', lastMonday));
$( "#dateFin" ).val($.datepicker.formatDate('dd/mm/yy', lastSunday));
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 week from selected week/year using Moment.js?
Assuming the first day is Monday, here is your solution:
function calculateDateFromWeekNumAndYear(year, week) {
const firstDate = moment().day('Monday').year(year).week(week).format('YYYY-MM-DD');
const lastDate = moment(firstDate).add(6, 'days').format('YYYY-MM-DD');
const dateRange = [firstDate, lastDate];
return dateRange;
}
How to get first and last day of the week with Moment.js?
You are on the right track. The weekday
function can be used to get the Monday and Friday of the current week.
var now = moment();
var monday = now.clone().weekday(1);
var friday = now.clone().weekday(5);
var isNowWeekday = now.isBetween(monday, friday, null, '[]');
console.log(`now: ${now}`);
console.log(`monday: ${monday}`);
console.log(`friday: ${friday}`);
console.log(`is now between monday and friday: ${isNowWeekday}`);
Direct link to the Moment.js docs on the weekday
function here: https://momentjs.com/docs/#/get-set/weekday/
Related Topics
:Touch CSS Pseudo-Class or Something Similar
Convert Relative Path to Absolute Using JavaScript
How to Get Old Value with Onchange() Event in Text Box
How to Separate Web Components to Individual Files and Load Them
Detect Click Event Inside Iframe
How to Save and Restore a File Object in Local Storage
Opening New Window in HTML for Target="_Blank"
How to Listen/Detect Changes to an Input Value - When the Input Value Is Changed via JavaScript
How to Reach the Element Itself Inside Jquery's 'Val'
Addeventlistener with Ajax Not Working Well
How to Prevent an Http Request Just for a Favicon
HTML - How to Show Tooltip Only When Ellipsis Is Activated
How to Play a Notification Sound on Websites
JSON.Stringify Output to Div in Pretty Print Way
Should I Use Window.Navigate or Document.Location in JavaScript
How to Make a Radio Button Unchecked by Clicking It
Keydown Simulation in Chrome Fires Normally But Not the Correct Key