views:

126

answers:

4

Hi,

I have the following JSON object which has a date field in the following format:

{
    "AlertDate": "\/Date(1277334000000+0100)\/",
    "Progress": 1,
    "ReviewPeriod": 12 
}

I want to write a regular expression or a function to convert it to a javascript object so that it is in the form:

{
    "AlertDate": "AlertDate":new Date(1277334000000),
    "Progress": 1,
    "ReviewPeriod": 12 
}

The above date format fails validation in the JQuery parseJSON method.

I would like to convert the 1277334000000+0100 into the correct number of milliseconds to create the correct date when eval is called after validation.

Can anyone help me out with a good approach to solving this?

Cheers

Paul

A: 

You should really not use eval unless you really need to; why not just have the seconds directly in the JSON, and call Date on that number when you need to format?

If you must, you can parse out the number out of the string using regex

azatoth
+4  A: 

Strip the irrelevant parts out using String.replace(), parse the timestamp using parseInt and construct a new Date() with the timestamp.

var json = {
    "AlertDate": "\/Date(1277334000000+0100)\/",
    "Progress": 1,
    "ReviewPeriod": 12 
};

json.AlertDate = new Date(parseInt(json.AlertDate.replace(/(^.*\()|([+-].*$)/g, '')));

alert(json.AlertDate);

Explanation of /(^.*\()|(\+.*$)/g:

  • /.../ = start and end of JS regex pattern.
  • ^.*\( = matches everything from beginning until with first (. The \ is used to escape (.
  • [+-].*$ = matches everything from first + or - until with end.
  • (..)|(..) = do one or other.
  • /g = global modifier, will replace both.

See also:

BalusC
Your regex won't work if the time offset is negative ( e.g. "/Date(1277334000000-0100)/" )
fudgey
@fudgey: `parseInt` by coincidence already takes this into account, but you're right, this is not nice, I've updated the answer.
BalusC
A: 

Is this what you're looking for?

If this is one item in your object: o = { "AlertDate": "\/Date(1277334000000+0100)\/", "Progress": 1, "ReviewPeriod": 12 }

This code will extract the first value number (ignoring the "+0100"), convert to a number and create the date object.

var rxFirstNumber = /(\d+)/;
var strAlertDate = o.AlertDate;
var arrMatches,intTimeStamp;

arrMatches = strAlertDate.match(rxFirstNumber);
if (arrMatches !== null && arrMatches.length > 0) {
    intTimeStamp = parseInt(arrMatches[1],10);
    o.AlertDate = new Date(intTimeStamp);
}

If you can trust your data to always contain that string data (or at least that AlertDate will always be a string containing a number), this can be expressed in a single line of (nasty & unmaintainable) code:

o.AlertDate = new Date(parseInt(o.AlertDate.match(/(\d+)/)[1],10));
Andrew
A: 

I need a more global answer than just changing the date for an individual property.

I need to change all the dates in a JSON string and not just on one property.

I ended up with the following regular expression

data = data.replace(new RegExp('\\"\\\\\/Date\\((\\d{13}\\+\\d{4})\\)\\\\\/\\"', 'g'), "new Date($1)");
dagda1