0

I am assuming that , If I run this query on

> (new Date()).getTime()
1443104144268

on Node console or JS console then I will get this timeStamp. As per my understanding, irrespective of Location of console or timezone, this timestamp is always same. If at one particular instance, everybody in the world run this statement, then everybody will get same timestamp.

In my project, I need to convert this timeStamp (1443104144268) to YYYY-MM-DD (2015-09-24) format.

But I do not want to use any existing Date/Time library of System library.

Do anybody know how to convert this timestamp to YYYY-MM-DD (2015-09-24) format without using Date function in JavaScript. ?

Narendra Sisodiya
  • 381
  • 2
  • 5
  • 13
  • The JavaScript [Date API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) has all the tools you need to do that. It makes no sense to avoid it. – Pointy Sep 24 '15 at 14:24
  • Also it's not exactly true that timestamps are always identical when queried simultaneously - system clocks are not perfect. – Pointy Sep 24 '15 at 14:26
  • In summary: how is the timestamp value derived/calculated from a date object, so that it can be reversed and derive a date from a timestamp: see here: http://stackoverflow.com/questions/3309012/what-is-the-formula-for-calculating-a-timestamp – freedomn-m Sep 24 '15 at 14:29
  • (I've had another look at that question and it's not explicitly for javascript, you can try it out to see if you get the expected result) – freedomn-m Sep 24 '15 at 14:31
  • My server receive, YYYY-MM-DD format. new Date('2015-10-24') thing gives me different results when my location of server changes. I need to add one day in 2015-10-24, for adding one day, I need to convert and date Object and then again to YYYY-MM-DD format. In timezone sensitive application, I am getting problems because of client server and database are running on different different server. after huge frustration, I have decided to use YYYY-MM-DD from server to client and everywhere without dealing with timezone at all. – Narendra Sisodiya Sep 24 '15 at 14:33
  • 1
    Why not just use UTC times? – Pointy Sep 24 '15 at 14:34
  • If you're using js, then the date and/or timestamp will always be based on the *client*'s time. If I make a request, then change my clock to an hour ago, and make another request, they timestamps will show that my second request was before my first. The format of the date/timestamp makes no difference. The only solution is to have a timestamp provider on your server, so the client can get a timestamp that will not be based on the time of the client machine. – freedomn-m Sep 24 '15 at 14:42
  • If your issues are due to timestamps, then just always use UTC (as agreed by @Pointy) - the js Date API has methods to output as UTC and will convert from the local timezone automatically. Or just output without a timestamp. – freedomn-m Sep 24 '15 at 14:43
  • 1
    Weird, useless question, really. – Nick Song Sep 24 '15 at 14:47
  • actually, more than 90% case, in almost every application, specific time zone + time instant information is not needed. because, most of the attributes are day wise, for example revenue on a given day etc. So, storing them as Date Object which has huge information is not needed. So this is the reason, my client is sending `2015-10-24` to server rather than some kind of Stringify version of Date Object. I need to jump from 2015-10-24 to 2015-10-25. I am now writing algo to reach from 2015-10-24 to 2015-10-25. – Narendra Sisodiya Sep 24 '15 at 14:48

0 Answers0