Home > Net >  How to get stripe subscription current_period_end as date
How to get stripe subscription current_period_end as date

Time:03-12

My end goal is to check subscription.cancel_at_period_end for false and store subscription end date as a moment date object.

const subscriptionEndDate = moment(subscription.current_period_end);

This is the result field coming from stripe in test mode.

current_period_end: 1649650039

But even using new Date(subscription.current_period_end) is coming back as 1970-01-20T02:14:10.039Z

Is this not the field that is suppose to show when the next billing date is ?

Any thoughts? What am I missing ?

UPDATE:

I was just doing some testing and figured out if I multiply that value by 1000 it comes out to be 1649650039000 which equates to

Mon 11 April 2022 00:07:19

Is there a reason for this? Is this a safe method moving forward?

UPDATE:

I accepted answer below and am providing momentjs that converts directly to unix timestamp.

const date = moment(new Date()).unix();

CodePudding user response:

Issue

  • Stripe reports date fields as Unix timestamps. These represent a date/time as the number of seconds since January 1, 1970 (kinda...leap seconds are weird).
  • The Javascript Date object attempts to convert the number of milliseconds since January 1, 1970 as that is an increment of time that is more relevant to front-end web coding.

Solution

You have already discovered an adequate solution, that is multiply the timestamp by 1000 and thereby convert the value in seconds to a value in milliseconds. This appears to be a common work around 1, 2, 3

  • Related