Skip to main content
19 events
when toggle format what by license comment
Mar 15, 2023 at 6:46 comment added Wernfried Domscheit The linked library is very limited, e.g. it supports only time zones Pacific|Mountain|Central|Eastern|Atlantic. I would suggest a decent Date library like Moment.js, Luxon or Day.js
May 23, 2016 at 9:23 comment added IMSoP For anyone wondering why the eval is such a bad idea, try running the code in this answer with jsonDate = 'alert(\'NerNer\');/Date(1234567890123)/';
Nov 9, 2014 at 10:16 history edited Peter Mortensen CC BY-SA 3.0
Copy edited.
Feb 12, 2014 at 13:16 comment added gpvos @BrainSlugs83: there is a security issue. If the string does not match the regex, or has stuff before or after the matched bit (which, after all, is not anchored with ^ and $), arbitrary text can be passed to eval. You could wrap an if statement around it, but then you need to be careful that that condition only matches when the replace regex will match, and not more often. This only goes to show that security is tricky. If you just don't use eval, you don't need to think about all these things. Your life will be easier and you will write more secure software.
May 15, 2013 at 6:36 history made wiki Post Made Community Wiki by Venemo
Jul 13, 2012 at 4:35 comment added BrainSlugs83 There is no security issue with the eval here, since you're only passing in \d+. But this answers seems just as broken as the accepted one as they both ignore timezone information. (Actually, this one appears as though it will fail if timezone information is present.)
Dec 24, 2011 at 8:57 review Suggested edits
Dec 24, 2011 at 9:22
Jun 23, 2011 at 21:15 comment added Roy Tinker @Edy: That is another form of eval, and is just as 'evil'. Parse the string instead (see my answer below)
Nov 11, 2010 at 21:02 comment added Roy Tinker @Gad: regex replacements are slow.
Sep 21, 2010 at 11:56 comment added Marcel Korpel @Gad: please don't post code in a comment; just formulate a new answer.
Sep 21, 2010 at 11:55 comment added Marcel Korpel @Edy: new Function is almost as bad as eval: dev.opera.com/articles/view/efficient-javascript/…
May 30, 2010 at 20:22 comment added Gad D Lord Based on what was suggested I made it this way: /** * Convert a Json datetime to Date object * @param {Object} jsonDateTime For example /Date(1224043200000)/ * @return {Date} return Date object */ Date.prototype.fromJson = function(jsonDateTime) { var date = new Date(parseInt(jsonDateTime.replace(/^\/Date((\d+))\/$/gi, '$1'), 10)); return date; };
Mar 29, 2010 at 10:39 comment added Ed Gomoliako 2pst: Instead of eval — (new Function('return ' + json))()
Feb 25, 2010 at 15:43 comment added Mark Rogers pst was correct, it is possible to do this in a variety of ways without 'eval'. Crockford says that 'eval Is Evil' because it is less readable and is less secure, furthermore he may further imply that it is less efficient and more dangerous because it hits the javascript compiler.
Jan 19, 2010 at 5:02 comment added eyelidlessness @rball, nonsense: jsonDate = new Date(+jsonDate.replace(/\/Date\((\d+)\)\//, '$1'));
Aug 27, 2009 at 15:19 comment added andreialecu There's nothing wrong with the line, the sequence is \// . First slash is escaped so it does not count like a comment. It's your editor tricking you, the line will work fine.
Jul 21, 2009 at 19:08 comment added orandov I think there is something wrong in that line of JS. The last two slashes act as comments. I don't know regexp enough to fix it.
Oct 16, 2008 at 13:24 vote accept Mark Struzinski
Nov 27, 2011 at 5:23
Oct 15, 2008 at 21:14 history answered Panos CC BY-SA 2.5