I'm trying to figure out the best way to handle expiration dates for coupon codes that need to go by local time. For example, we have events that occur at specific times in various locations worldwide, like an event in Melbourne at 11am on May 16, 2025. That's straightforward because we can store it in ISO-8601 format and adjust as needed. However, I'm stumped when it comes to a coupon code that expires at midnight in the buyer's local time on the same date. What's a good way to store and convey this fuzzy expiry date clearly for developers and business people, especially since this could mean different actual times in various regions? I'm not concerned about VPN issues; I just want to ensure clarity in our data representation.
5 Answers
Why not just set the expiry as a date only, without worrying about the time? Then, when the user tries to use the coupon, you can check the exact date and time based on their timezone. This keeps it simple and clear!
Remember, Google is your friend! The localization of dates and times can be tricky, but there are tons of great tutorials out there that provide solid solutions in various programming languages.
Consider storing the timestamp in the database as a Unix epoch time. When you need to display or check it, format it using a library like dayjs, moment.js, or date-fns to handle locale settings.
Store the time in ISO 8601 format including the offset. For instance, you can use something like `2025-05-16T04:29:45Z` for UTC or `2025-05-16T16:29:45+12:00` for a specific timezone. If you know the timezone for the coupon, save it at midnight in that timezone instead of converting it all to UTC. If you're unsure of where the user will be, you can just save the date without an offset. This way, when it’s checked locally, use the client’s local timezone for verification.
If the exact expiration time isn't critical, I’d just use Pacific Time for North America since it gives some leeway. Anyone further east would essentially get more hours to use the coupon. It's better to have a little wiggle room than to lose out on a sale.
That’s basically what we've been doing too. But since we cater to countries like the UK and Australia along with North America, it’s a bit tricky with all the time differences. Accuracy is becoming more vital as our profit margins shrink.