Get GMT time difference between now GMT and 1 January 2013 gmt

I am trying to store the time as a difference between 1 jan 2013 and the moment i save it on the database. It is definitely smaller than the standard of making it relative to 1970 ;) both in storage and because i am sending it clientside.

As I am going to do a getRelativeTime() quite a lot, I am looking for simplicity. I might change it from 1 jan 2013 to the date of server launch which is the 1st of a month in 2013 :D

I am keeping client and serverside track in GMT and it is on a nodejs server, the server is unknown. From what I researched, there are some solutions, but many call them incorrect due to this and that :|

To offer a valid answer to your question:

var ms = new Date() - Date.UTC(2013,0,1);

But PLEASE do not do this.

  • You will end up confusing anyone that uses your system (developers, api clients, database folks, etc.)

  • You will not be saving as much in terms of bytes as you think. JavaScript Number types are always 64 bits (8 bytes) in memory, see here. How much you might save in the database is very dependent on your database platform, but for example, the MySQL DATETIME type takes 8 bytes, while the TIMESTAMP type takes only 4. The range of a TIMESTAMP is much smaller, reference here and here.

  • You will probably encounter negative numbers frequently, if your app works with dates before your epoch. They will work, but they could be a source of confusion. If someone isn't aware of your adjusted epoch, they could produce a date that looks valid, but is supposed to mean something different.

  • You will also throw out any potential for using many great libraries that expect dates to work a certain way. You could convert to and from your custom date to a standard one, but do you really want to constantly wrap and unwrap those values? Probably not. Even libraries like moment.js that use their own types, do so by extending or encapsulating the standard data types. Compatibility is a much higher concern than byte space.

There are a lot of things wrong with JavaScript dates, but the choice of 1/1/1970 as an epoch was a good thing. Just because you can do something, doesn't mean that you should.

Also, consider that disk space is usually the least expensive component to running an application. Even with millions of records, you are likely to save a few dozen megabytes at most. Let's just error on the high side and say you save 200 MB with this clever hack. Amazon's top-tier provisioned IOPS cloud storage is $0.125 per GB per month. Congratulations, you've saved $7.68 over the entire year.

You are trying to normalize the value of epoch time? Epoch time as such gives in positive integer values. ain't it? 1356998400 is the amount of data you want to normalize.