1800 INFORMATION
1800 INFORMATION

Reputation: 135265

How to have a format string with "start of week" and "end of week"

I want to take a date object and format it so that the output result is something like this:

2015: 23 - 30 July

The format string would be something like this:

YYYY: ii - jj MMMM

But what do I put in for ii and jj? ii should be something that will evaluate to the day which is at the start of the current week, and jj will evaluate to the day which is at the end of the current week.

My current code:

console.log(moment().format('YYYY: ii - jj MMMM'));

Upvotes: 3

Views: 356

Answers (2)

Ruslanas Balčiūnas
Ruslanas Balčiūnas

Reputation: 7418

Why not concatenate two strings?

moment().startOf('isoweek').format('YYYY: DD') + ' - ' + moment().endOf('isoweek').format('DD MMMM')

<htm>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js">
  </script>
  <script>
    document.write(moment().startOf('isoweek').format('YYYY: DD') + ' - ' + moment().endOf('isoweek').format('DD MMMM'));
  </script>
</htm>

Upvotes: 1

Alexander Elgin
Alexander Elgin

Reputation: 6956

function getWeekRange(date) {
    return moment(date).format('YYYY') + ': ' + moment(date).startOf('week').format('D') + ' - ' + moment().endOf('week').format('D MMMM');
}
getWeekRange(new Date());

Upvotes: 2

Related Questions