0

I want to verify Date is valid for subscription.

Date is in UTC format.

when user creates an account i set the expiration date to be the date in 30 days.

Before each action, i want to verify the expiration date of his account.

To get the days left for user i do

let oneDay = 24*60*60*1000;
let daysLeft = (userSubscription.expired - new Date())/(oneDay))

Now i want to check that if daysLeft is 0, then do some action alerting the user.

My problem is, that is if expiration was a year ago, then days left will not be below 0 as i expected, it will be 300+.

how can i enforce it?

Yoni Mayer
  • 1,212
  • 1
  • 14
  • 27

1 Answers1

1

With this you just get the difference between those dates:

 let oneDay = 24*60*60*1000;
 let daysLeft = (userSubscription.expired - new Date())/(oneDay))

So the result after one year would be 365 - 30 = 335

But you need to check if userSubscription.expired is higher than new Date()

Use something from here: Compare two dates

Jonas
  • 2,139
  • 17
  • 38