Display days to account expiration using luxon
1 min read

Display days to account expiration using luxon

I wanted to let users know how many days left in their trial subscription they had left. Using the luxon package, I was able to format the date correctly by getting the difference between the user's creation date and 7 days.

After installing luxon with npm install luxon --save, I wrote the below function.

import { DateTime } from 'luxon'

daysToAccountExpiration(userCreationDate) {
    const created = DateTime.fromISO(userCreationDate)
    const expiration = created.plus({ days: 7 })
    const daysLeft = Math.floor(expiration.diff(DateTime.now(), ['days']).as('days'))
    return daysLeft
  }