最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Convert Json date string to JavaScript date object - Stack Overflow

programmeradmin4浏览0评论

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?

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 edited May 22, 2012 at 14:32 user229044 239k41 gold badges344 silver badges346 bronze badges asked Jun 22, 2010 at 23:52 dagda1dagda1 28.8k67 gold badges255 silver badges477 bronze badges 1
  • 1 I think you wrote "AlertDate": twice. – Marcel Korpel Commented Jun 22, 2010 at 23:56
Add a ment  | 

5 Answers 5

Reset to default 1

Reusable jQuery extension that auto-parses dates

I've written a jQuery extension (in case you do use one and I hope/remend 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.

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

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));

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)");

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, '')));
发布评论

评论列表(0)

  1. 暂无评论