Roxx
Roxx

Reputation: 3986

Days remaining in specific date for every month

At the moment I don't have any code, because I don't know where to start.

I have to manage subscription on my app. So, if user installed the app on 8th Sep then it will get renewed automatically on 8th October similar for every month. But I would like to show days remaining.

I have stored the date of installation in database. For one month I can do that easily using moment.js or angular way. Like below.

var a = moment([2017, 09, 29]);
var b = moment([2017, 10, 28]);
 Math.abs(a.diff(b, 'days'))+1

How can I achieve this?

More explanation

I don't need to get the current date. I want to show renew days for subscription. If someone installed on 4th Aug then it get renewed automatically on 4th Sep and next renew date will be 4th Oct. So, I want to show days remaining till 4th Oct.

Upvotes: 0

Views: 211

Answers (1)

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6887

Here is the jsfiddle link

var last_renewed_date = moment([2017, 07, 29]);//aug 29

var next_renewal_date = last_renewed_date.add(1, 'month');//sep 29

today = moment(); //today is sep 9
var diff_in_days = next_renewal_date.diff(today, 'days');
console.clear();
console.log("last_renewed_date",last_renewed_date)
console.log("next_renewal_date",next_renewal_date)
console.log('diff_in_days', diff_in_days); //20 days left

https://jsfiddle.net/brfq9rea/1/

Upvotes: 1

Related Questions