1

I have the following input and i can change the source of this data

Input

var strDate = "/Date(1391402871117+0100)/";

I can convert it to a date using eval, but i really dont want to eval

var DateResult1 = eval ("new Date(1391402871117+0100)");
console.log(DateResult1); // Date {Mon Feb 03 2014 05:47:51 GMT+0100 (Romance Standard Time)}

I did try this, sadly do not work:

// Remove /Date( )/
strDate = strDate.replace(/[^\d+]/g,'');

var DateResult3 = new Date(strDate);
console.log(DateResult3); //Date {Invalid Date}

When i write result of strDate i manual with out " it work.

var DateResult2 = new Date(1391402871117+0100);
console.log(DateResult2); // Date {Mon Feb 03 2014 05:47:51 GMT+0100 (Romance Standard Time)}

How can convert the input data into a date with out using eval or any library?

2
  • Very ugly solution new Date(+"/Date(1391402871117+0100)/".slice(6, -2).split("+").reduce(function (a, b) { return +a + +b; })); Commented Feb 3, 2014 at 12:22
  • This article may be of interest: hanselman.com/blog/… Commented Feb 3, 2014 at 12:43

3 Answers 3

2

You are very likely not getting a correct result out of this code:

var DateResult2 = new Date(1391402871117+0100);

The problem is the addition: 1391402871117+0100. 0100 is an octal constant, equal to 64 in decimal, which would add 64 milliseconds to the 1391402871117 timestamp. It seems likely to be indended as a time zone instead, but the Date constructor does not support time zones — only UTC and the local time zone of the browser.

Since UNIX timestamps are actually absolute (they are always in UTC), using just the timestamp would result in a Date instance referencing the correct instant in time, but possibly at another time zone. You can disregard the +0100 part, by converting the "1391402871117+0100" into an integer using parseInt:

strDate = strDate.replace(/[^\d+]/g,'');
var DateResult2 = new Date(parseInt(strDate));
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe i'm wrong but 1391402871117+0100 is same as 1391402871117+100 so if you don't parse octal as decimal it simply removes first 0.
The JS code itself is new Date(1391402871117+0100). You don't have to parse octal — the Javascript engine of the browser does, and it indeed does parse octal constants as octal. You can try it in the JS console: 0+0100 would give you 64, not 100 as it would if the 0100 was parsed as decimal 100.
1

If you can change the data source, as you say, why not do this?

Have your data source generate something like this, to add the timezone offset to the timestamp:

// convert timezone offset hours into seconds and add them to the timestamp
return (unixTimestamp + (timezoneOffsetHours * 3600)); 

Then you can do something like this in your JS:

// Math.floor works faster than parseInt to convert a string to integer :)
var timestamp = Math.floor(result of above timestamp generation);
var DateResult = new Date(timestamp);

The reason:

new Date() can't handle timezones specified in this way (or at all as far as I can Google)

Comments

0

try by parsing string to int:

var strDate = "/Date(1391402871117+0100)/";
strDate = strDate.replace(/[^\d+]/g, '');
var DateResult3 = new Date(parseInt(strDate.split('+')[0]) + parseInt(strDate.split('+')[1]));
console.log(DateResult3); 

Here is Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.