1

I'm setting up a system that needs to have posts that expire, and I need to compare the two to determine if it's expired. I'm using Meteor so I can do this on the clientside or serverside (i'm assuming the latter is preferred).

Looking into the MDN, i'm not quite sure how to store the timestamps and what format to use. It seems like the most naive way would be to use Date.now() for the current time and add 30 days in ms for the expired timestamp. Is there a better way to do this?

postModel = {
  id: String
  createdOn: Date.now()
  expiresOn: Date.now() + 2592000000 // 30days
}


post = getPost()

 if (post.expires is after now) {
   // throw expired error
 }
SkinnyGeek1010
  • 536
  • 7
  • 18

1 Answers1

2

You should store the expiration time span and not the actual date in a configurable manner. This can be a configuration file or database or some other persistence mechanism. For Your business entity you need to store the creation time only. This way expiration can be easily calculated on either the client or server side. Hard coding values like expiration is not a good idea.

Also, you should use UTC instead of local time. Always plan for success!

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • Thanks Paul, that's a good point. As far as calculating it seems like [this post](http://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates-using-javascript) would work for diffing. I generally feel confused about using dates, is there a good source to learn more about this? – SkinnyGeek1010 Jun 19 '14 at 21:24
  • Good question. There ought to be a source. .. My suggestions come entirely from experience. Here's some rules of thumb: If you hard code a value that represents a business rule, don't. If it can be easily calculated then don't store it. Always use UTC and yes, this will cause you to have to know the user's timezone. Remember that dates and times can be data or metadata or both. Your created DT is both actually but it won't be data until you convert it to the user's TZ. – Paul Sasik Jun 19 '14 at 21:48