omega
omega

Reputation: 43933

How to get end of week in javascript?

In javascript, well node.js, if I have a date object, how can I get the date object of the end of the week, like if the day of the initial object was Wednesday (regardless of time), then how can I get the date object but with it moved 4 days up and to 12:00 am, so Sunday midnight.

Thanks

Upvotes: 0

Views: 131

Answers (2)

Rayon
Rayon

Reputation: 36609

Try this:

var dateObj = new Date();
var day = dateObj.getDay();
var diff = 7 - day;
var neDate = new Date();
neDate.setDate(dateObj.getDate() + diff);
document.getElementById('data').innerText = neDate;
<div id='data'></div>

Upvotes: 0

m.antkowicz
m.antkowicz

Reputation: 13581

use moment.js and its endOf() function

    var m = moment(new Date(2011, 2, 12, 5, 0, 0)); 
    m.endOf("week");

Upvotes: 1

Related Questions