Reputation: 11030
My date range is
var currDay = Jan.1
var birthday = Feb.15
I know that to find the difference in number of weeks is
currDay.diff(birthday, 'week')
However, is there a way to find the full weeks and the remaining days?
Upvotes: 4
Views: 9206
Reputation: 5967
You can make use of duration
.
You can get the years, months (excludes years) and days (excludes years and months) using this. Only problem is weeks are calculated using the value of days so you'd still have to get the remainder on days if you're getting the number of weeks.
From momentjs docs:
Pay attention that unlike the other getters for duration, weeks are counted as a subset of the days, and are not taken off the days count.
Note: If you want the total number of weeks use asWeeks()
instead of weeks()
var currDay = moment("2018-01-01");
var birthday = moment("2018-02-16");
var diff = moment.duration(birthday.diff(currDay));
console.log(diff.months() + " months, " + diff.weeks() + " weeks, " + diff.days()%7 + " days.");
console.log(Math.floor(diff.asWeeks()) + " weeks, " + diff.days()%7 + " days.");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Upvotes: 8
Reputation: 13669
var currDay = moment("Jan.1","MMM.DD");
var birthday = moment("Feb.15","MMM.DD");
var diff = moment.duration(birthday.diff(currDay));
console.log(diff.weeks() +" weeks & "+ diff.days()%7 +" days to go for birthday");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Upvotes: 0