2

Im using momentjs in a nodeapp and then trying to find the difference between 2 dates in weeks like below,

var serviceCreated = moment(new Date(service.createdAt)), //fetching this value from DB
    currentDate    = moment(new Date()),
    delta          = currentDate.diff(serviceCreated, 'weeks') //it returns the value as integer

As mentioned in the DOC if i have stated currentDate.diff(serviceCreated, 'weeks', true) then it provides a float value say for example if the difference is 4 weeks and 4 days i would expect this value to be some where around 4.4XXXX so i can grab the value after the decimal place and then have it as 4 days. But this logic is not working all the times there is a mismatch here and there.

So bottom line is via momentjs can i get the difference in weeks and days at the same time, if that's possible then i can add those days which might range from 1 to 6 to the delta value.

I also did try something with http://momentjs.com/docs/#/plugins/preciserange/ but that gives me the total days and the other one gives me the total weeks, i'm trying to get the difference in weeks and days at the same time.

Example:

var serviceCreated = moment(new Date('08/02/2016')),
    currentDate    = moment(new Date('10/16/2016'));
    currentDate.diff(serviceCreated, 'weeks', true) // returns 10.633490008267195

which i assume is 10 weeks and 6 days but in actual the difference between the above dates is 10 weeks and 5 days (without including the end date)

Sai
  • 1,790
  • 5
  • 29
  • 51
  • "But this logic is not working all the times there is a mismatch here and there" how so? Do you have an example of it not working? – Rob M. Aug 02 '16 at 17:14
  • Just added an example in the original query. – Sai Aug 02 '16 at 17:39
  • 2
    You are incorrect in your assumption that `10.63` of a week is 10 weeks and 6 days. It's 10 weeks and 0.63 of a week, which is `(7 * 0.63)` == ~4.4 days... – Heretic Monkey Aug 02 '16 at 17:47

0 Answers0