7

My scenario is to convert the data of milliseconds to a date format. I have tried a lot as here http://jsbin.com/jeququ/1/ but it is only working for a limited milliseconds value, and fails if the value is high. I am looking to replicate the functionality as http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/#.VJAp9lWUe1R

Milliseconds Since : Thu Jan 01 1970 05:30:00 GMT+0530

user3592479
  • 695
  • 3
  • 13
  • 26

2 Answers2

16

This should work:

var d = new Date(milliseconds);
Sagar Koshti
  • 1,446
  • 2
  • 9
  • 3
  • I am already using the same . But how do we take a reference date . Required reference date is Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time) – user3592479 Dec 16 '14 at 13:41
  • @user3592479 Your reference date is the same as 1st January 1970 00:00:00 UTC, so this will work fine for you. – Timespace Dec 16 '14 at 14:56
  • @userTimespace , but how can we take any other date as reference – user3592479 Dec 16 '14 at 17:40
  • @user3592479 As far as I know, there isn't a function built in to JavaScript that lets you do that. You could write your own, or find a library that lets you do that. Though this is different enough from your original question that I'd advise asking a new question if you need to use a different reference date (and someone else might prove me wrong!) – Timespace Dec 17 '14 at 16:21
  • Ok. But is there any significance to take 1970 year as default? – user3592479 Dec 17 '14 at 17:36
  • 1
    @user3592479 Yes - 1st January 1970 00:00:00 UTC is the Unix Epoch. When expressing time as an integer in most programming languages, `0` is the Unix Epoch (and if you don't want this to be the case, you'll probably end up making your life more difficult than it needs to be). If your question is _why_ this is the default, perhaps [this question](http://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time) can help. – Timespace Dec 18 '14 at 16:39
9
var d = new Date(+milliseconds);
user3592479
  • 695
  • 3
  • 13
  • 26