get the date for the last day of this week in javascript

To get the date of the last day of the current week in JavaScript, you can use the getDay() method to get the current day of the week and then subtract that number of days from the current date.

Here's an example using plain JavaScript:

index.tsx
const today = new Date();
const lastDayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() + (6 - today.getDay()));

console.log(lastDayOfWeek);
166 chars
5 lines

This will output the date for the last day of the current week.

Alternatively, you can use a library like date-fns or Moment.js to make date manipulation easier.

Here's an example using date-fns:

index.tsx
const { lastDayOfWeek } = require('date-fns');

const today = new Date();
const lastDayOfWeek = lastDayOfWeek(today);

console.log(lastDayOfWeek);
147 chars
7 lines

And here's an example using Moment.js:

index.tsx
const moment = require('moment');

const today = moment();
const lastDayOfWeek = today.endOf('week');

console.log(lastDayOfWeek);
131 chars
7 lines

gistlibby LogSnag