0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • 1
    Just check how far they are away from the 8th on the next month??? If you can't figure it out, write it on a piece of paper, and see how you would add it yourself – Tyler Nichols Sep 08 '17 at 12:22
  • 1
    So all you need to know is the difference between two dates? If so, [this question](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) has your answer – Rory McCrossan Sep 08 '17 at 12:23
  • Problem is i will have to check for everymonth. and i only have installation date. – Roxx Sep 08 '17 at 12:24
  • 1
    Given the nature of the question your name is ironic... :) – Liam Sep 08 '17 at 12:24
  • 1
    In which case you need to get the current date. If day > 8, add one to the month, then change the day to the 8th. Now you have the date to calculate the difference – Rory McCrossan Sep 08 '17 at 12:25
  • @Liam should i take it as compliment or comment? – Roxx Sep 08 '17 at 12:26
  • 2
    Definitely a comment o_O – Liam Sep 08 '17 at 12:27
  • @RoryMcCrossan 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. – Roxx Sep 08 '17 at 12:28
  • What you exactly need is difference between current date & (installation date +30) . Am I correct ? – Deepansh Sachdeva Sep 08 '17 at 12:29
  • @Liam thanks thats what i understood. – Roxx Sep 08 '17 at 12:29
  • @CalculatingMachine you do need the current date as both the starting date, and to figure out when the next 8th of the month is... – Rory McCrossan Sep 08 '17 at 12:29
  • 1
    You'll have some edge cases, like if someone installed on 31 Jan, and renewed on 28 Feb, then the next renewal should be 28 or 31 Mar? – Icycool Sep 08 '17 at 12:30
  • @Icycool every 30 days. – Roxx Sep 08 '17 at 12:30
  • @Liam Name changed as suggested by you. :) – Roxx Sep 08 '17 at 12:30
  • That conflicts with what you said, from 4th Aug to 4th Sep is 31 days – Icycool Sep 08 '17 at 12:32
  • 2
    Every 30 days is easy, just find number of days between today and install date, then %30 – Icycool Sep 08 '17 at 12:33
  • @Icycool easy for you not for me. If it was easy for me then i would solved it by myself. – Roxx Sep 08 '17 at 12:34
  • I mean it is easier than same day of every month. sorry if it sounds offending. So, the requirement is every 30 days or same day of every month? – Icycool Sep 08 '17 at 12:35
  • @Icycool it wasn't offending. I am just saying that you guys have more experience than me. it is easy for you but not for me. – Roxx Sep 08 '17 at 12:36
  • So... what is the requirement? – Icycool Sep 08 '17 at 12:44
  • @Icycool i thought you are aware about it. and i think its good idea which you suggested above. – Roxx Sep 08 '17 at 12:45
  • @Icycool i am talking about "Every 30 days is easy, just find number of days between today and install date, then %30" – Roxx Sep 08 '17 at 12:48
  • Ah..ok. Just keep in mind that would mean only 360 days are covered in 12 renews =) – Icycool Sep 08 '17 at 13:01

1 Answers1

1

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/

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
  • If you want it to be in period of 30 days, you need to put last_renewed_date.add(30, 'days') so it will add 30 days on every renewal date – Alaksandar Jesus Gene Sep 08 '17 at 13:12
  • The issue here will be time difference. For example a user in India will see 20 days but the user in US will see 21 days. It depends on your server time for which you need to adjust moment with the timezones – Alaksandar Jesus Gene Sep 08 '17 at 13:14
  • Thanks for your answer. Let me try. Voted up. – Roxx Sep 08 '17 at 13:50