Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Last week date range from sunday to saturday moment js

I need to get last week date range from sunday to saturday, but this code

moment().subtract(1, 'weeks').startOf('isoWeek')
moment().subtract(1, 'weeks').endOf('isoWeek')

gives date range from monday to sunday, How to get Last week date range from sunday to saturday with moment js?

like image 799
mano10 Avatar asked Aug 01 '18 12:08

mano10


1 Answers

isoWeek gets you the start of week as per ISO 8601 standard. According to international standard ISO 8601, Monday is the first day of the week.

You may try using week instead of isoWeek in the method, which works as per your system settings (locale).

console.log(moment().subtract(1, 'weeks').startOf('isoWeek').format('dddd'));
console.log(moment().subtract(1, 'weeks').endOf('isoWeek').format('dddd'));

console.log(moment().subtract(1, 'weeks').startOf('week').format('dddd'));
console.log(moment().subtract(1, 'weeks').endOf('week').format('dddd'));

console.log(moment().subtract(1, 'weeks').startOf('week').format('YYYY-MM-DD'));
console.log(moment().subtract(1, 'weeks').endOf('week').format('YYYY-MM-DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Read the difference here.

like image 127
31piy Avatar answered Oct 25 '22 02:10

31piy