alex
alex

Reputation: 309

How to return a Date format when I use setDate()

I assigned the weekEnd as the current end of the week like this

this.weekEnd = new Date(this.currentDate.setDate(end));

Next, what I want is to give a new value to the weekEnd which is the weekEnd + 7 days. I've done it like below but I can't assign the new value to the weekEnd because the right side returns a Number and not date.

this.weekEnd = this.weekEnd.setDate(this.weekEnd.getDate() + 7);

If you guys have any idea how I can do it I would appreciate it. Thanks a lot!

Upvotes: 1

Views: 1011

Answers (2)

Carsten Massmann
Carsten Massmann

Reputation: 28246

Just use the .setDate() method without the assignment:

this.weekEnd.setDate(this.weekEnd.getDate() + 7);

.setDate() does two things:

  1. It changes the current Date object to the new date.
  2. Returns the number of milliseconds of that new date since 1 Jan 1970.

In your code you assigned this number to the variable that would have held the correct date anyway.

Upvotes: 3

Wahab Shah
Wahab Shah

Reputation: 2276

Try this. It will return 15th Jun and 21st June. If you remove +1 then it will start from sunday. To start from monday you have to add 1.

let current = new Date;
let firstday = new Date(current.setDate(current.getDate() - current.getDay()+1));
let lastday = new Date(current.setDate(current.getDate() - current.getDay()+7));

Upvotes: 0

Related Questions