Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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": 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?

share|improve this question
1  
I think you wrote "AlertDate": twice. –  Marcel Korpel Jun 22 '10 at 23:56
add comment

5 Answers

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

share|improve this answer
add comment

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));
share|improve this answer
add comment
up vote 0 down vote accepted

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)");
share|improve this answer
add comment

Reusable jQuery extension that auto-parses dates

I've written a jQuery extension (in case you do use one and I hope/recommend you do) that changes $.parseJSON() functionality so it's able to automatically parse dates for you. No need for repetitious code for date parsing any more.

Check my blog post with code.

share|improve this answer
add comment

Hello i just want to Add to BalusC answer:

If you want Dates not being undefinied for certain older dates instead of

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

You Could :

var dateObject = new Date(new Number(yourDateVariable.replace(/(^.*\()|([+-].*$)/g, '')));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.