0

My javascript Date received like this.

trip.Date: "/Date(1426530600000)/"

How can i convert this to Date format

I tried this working:

var dt = new Date(1426530600000); // output: Tue Mar 17 2015 00:00:00 GMT+0530 (India Standard Time)

Is there any other predefined methods to convert this "/Date(1426530600000)/"

cweiske
  • 30,033
  • 14
  • 133
  • 194
Vicky
  • 819
  • 2
  • 13
  • 30
  • There's no predefined way. If new Date(1426530600000), what's the issue in using that only. – Bikas Mar 17 '15 at 11:14
  • This looks like its a string made for `eval`. Now dont go using eval! The best way for this is to get the numbers out and run it through the Date function yourself. So something like `new Date( parseInt(Date(1426530600000).split("(")[1]) )` should do the trick. – somethinghere Mar 17 '15 at 11:16
  • /Date(1426530600000)/ How can i split the number from this string – Vicky Mar 17 '15 at 11:26

3 Answers3

1

Use for example an RegEx expression such as this, then:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("/Date(1426530600000)/");  // insert string
alert(new Date(parseInt(matches[1], 10)));           // parseInt() the string

A string split is a good alternative.

0

you create the date correctly. but then you have several methods to represent the date as string.

.toString() returns your local date and time

.toISOString() returns GMT time in the format yyyy-mm-ddThh:mi:ss.sssZ

for more see http://www.w3schools.com/jsref/jsref_obj_date.asp

Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
0

I like to use moment.js to handle with dates:

var timestamp = "1426530600000";

//Parse the timestamp to Moment
var dateAsMoment = moment(timestamp);

//parse your date to String (any formats that you need)
dateAsMoment.format('YYYY-MM-DD HH:mm:ss')

Or you can use your Date object:

var dt = new Date(1426530600000);
var dateAsMoment = moment(dt);
...

Edit:
To extract the timestamp from your 'eval' string:

//using a regex to get the contents between parenthesis 
var timestamp = "/Date(1426530600000)/".match(/\(([^)]+)\)/)[1]
Victor
  • 5,043
  • 3
  • 41
  • 55