JSON date format for jquery ajax request

I want to download some data via ajax and automatically sync dates.

var url = "http://example.com/report_containing_dates.json" jQuery.getJSON(url, function(data_containing_dates_and_strings){ console.log(date); }); 

The date format in my json is "2012-09-28" (the default is from rails to_json), but jQuery just processes this string. In what format should a date be used for jquery to parse it as a date?

Answer example:

 { "columns": [ ["date", "Date"], ["number", "active_users"], ], "rows": [ ["2012-09-28", 120, 98, 60], ["2012-09-29", 127, 107, 63] ] } 
+4
source share
4 answers

OK, that was a lot harder than expected, but I have a solution.

The approach I took is to request a custom data type in an ajax request, and then implement my own converter.

First of all, the format that I use for dates in my json now has a date ("yyyy-mm-dd"), the original example would look like this:

 { "columns": [ ["date", "Date"], ["number", "active_users"], ], "rows": [ ["date(2012-09-28)", 120, 98, 60], ["date(2012-09-29)", 127, 107, 63] ] } 

Then I register a converter to convert text to a custom data type called json_with_dates. The regular expression is used to find the date format and replace it with operators to create date objects. Then Eval is used to build json.

 jQuery.ajaxSetup({ converters: { "text json_with_dates": function( text ) { var with_dates = text.replace(/\"date\(([^)]*)\)\"/g, function(a, date){ var dateParts = date.split("-"); return "new Date(" + dateParts[0] + "," + dateParts[1] + "," + dateParts[2] + ")"; }); var converted = eval("(" + with_dates + ")"); return converted; } } }); 

Then I make an ajax request for the user data type:

 $.ajax({ url: div.data('chart'), dataType: 'json_with_dates', success: function(data_including_dates){ console.log("win!"); } }); 
+1
source

It doesn't matter how you format the date string. JSON methods will never automatically convert it to a Date object. JSON only supports these basic types: Number , String , Boolean , Array , Object and null . (Http://en.wikipedia.org/wiki/JSON)

You need to convert these date strings directly to Date objects.

In your case, it could be something like:

 $.each(response.rows, function (idx, row) { row[0] = Date.parse(row[0]); } 
+5
source

Use Date.parse , which will convert from string to date.

+2
source

It’s best to parse the date yourself. I ran into problems in some browsers without parsing the date from the string as you expected. Here's a quick prototype for use on line 2012-09-28 :

 String.prototype.parseDate = function(){ var date = this.split("-"); var yyyy = date[0]; var mm = date[1]; var dd = date[2]; date = new Date(yyyy,mm,dd); date.setMonth(date.getMonth()-1); // since Months are 0 based return date; } console.log(data.rows[0][0].parseDate()); console.log(data.rows[1][0].parseDate());​ 

EXAMPLE

Adapted from a similar question: IE JavaScript parsing errors

The Date.parse method is completely implementation dependent (the new Date (string) is equivalent to Date.parse (string))

0
source

Source: https://habr.com/ru/post/1439384/


All Articles