0

I have JSON from a SQL query [{date, text, desc},{,,,}] and I need to compare the date (date1, date2, ...) to multiple other JS date objects (b1, b2, ...) efficiently.

The problem is: currently I have to convert each JSON date into a JS date object before I can compare them, for example:

(date1->a1) then (a1 to b1)
else (date2->a2) then (a2 to b1)
else (daten->an) then (an to b1)
... then
(date1->a1) then (a1 to b2)
else (date2->a2) then (a2 to b2)
else (daten->an) then (an to b2)
... and again for each b

I am looking for a more efficient method of doing this conversion-comparison as there could be ~90 'a's and 30-90 'b's.

Additional info:

  • JSON dates are DESC
  • JS dates are ASC
  • The comparison is implemented like How do I format a Microsoft JSON date?
  • When the dates match, return the JSON object to be processed
  • If no dates match, return false
  • I do have an early exit if b > a, but early on many won't fall into this criteria

I am using this in a JS function to insert the 'text' field into a table for Google Charts.

Thank you so much in advance for taking a look at my problem!

Example JSON:

[
  {
    "id": 797,
    "title": "test",
    "description": "test",
    "annotationDate": {
      "date": "2013-06-02 00:00:00",
      "timezone_type": 3,
      "timezone": "America\/Los_Angeles"
    },
    "discr": "annotation"
  },
  {
    "id": 806,
    "title": "recent",
    "description": null,
    "annotationDate": {
      "date": "2013-06-01 00:00:00",
      "timezone_type": 3,
      "timezone": "America\/Los_Angeles"
    },
    "discr": "annotation"
  }
]

Example JS date objects:

Date {Sat Jun 01 2013 11:19:35 GMT-0700 (PDT)}
Date {Sun Jun 02 2013 11:19:35 GMT-0700 (PDT)}
Date {Mon Jun 03 2013 11:19:35 GMT-0700 (PDT)}
Date {Tue Jun 04 2013 11:19:35 GMT-0700 (PDT)}
Community
  • 1
  • 1
Tony
  • 1,128
  • 12
  • 23

1 Answers1

0

For each query-date, convert it to a number with Number(d), and store in a dictionary (JavaScript object). Loop trough the JSON-objects, and convert each date-string to a Date object, then to a number. Check if this number exists in the dictionary.

function filterCharts(json, dates)
{
    var origin = new Date("2000-01-01T00:00:00");

    var dateLookup = {};
    for (var i = 0; i < dates.length; i++)
    {
        var key = dates[i] - origin; // milliseconds
        key = Math.round(key / 86400000); // days
        dateLookup[key] = true;
    }

    var result = [];
    for (var i = 0; i < json.length; i++)
    {
        var jsonDate = new Date(json[i].annotationDate.date.replace(" ", "T"));
        var key = jsonDate - origin; // milliseconds
        key = Math.round(key / 86400000); // days
        if (dateLookup[key])
        {
            result.push(json[i]);
        }
    }

    return result;
}

I don't know how you want to manage the timezone, so I'll leave it up to you.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • Thank you so much, gave me the start for my variation on this implementation (stored the date itself as the key)! I must ask though, what is the data structure/difference between var x = {} vs var y = []? – Tony Jun 21 '13 at 21:13
  • Also, do you happen to know why I get `var jsonDate = new Date(json[i].annotationDate.date);` as an invalid date? I had to convert it toString and make the date using setFullyear, setMonth, setDate! – Tony Jun 21 '13 at 21:26
  • The Ecma-script standard defines valid date-strings as `` or `T – Markus Jarderot Jun 21 '13 at 22:51
  • `{}` is an empty JavaScript object. It can be used as a dictionary, with a few restrictions. `[]` is an empty list. – Markus Jarderot Jun 21 '13 at 22:55
  • Thank you, you've cleared it up for me and I will take your date-time standards into account when I go over my code again. Much appreciated! – Tony Jun 23 '13 at 06:05