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

Here's how you can get the date for the last day of this week using TypeScript:

index.ts
import * as moment from 'moment';

const now = moment(); // current date and time
const endOfWeek = now.endOf('week'); // last millisecond of current week
const lastDayOfWeek = endOfWeek.format('YYYY-MM-DD'); // formatted date string

console.log(lastDayOfWeek); // e.g. "2021-10-10"
284 chars
8 lines

In this code, we create a moment object representing the current date and time using the moment() function. We then use the endOf() function to get the last millisecond of the current week, and store that moment in the endOfWeek variable. Finally, we format that moment as a string using the format() function and the YYYY-MM-DD format string, and store the result in the lastDayOfWeek variable. We then log that variable to the console, which will output the last day of the current week in the specified format.

gistlibby LogSnag