My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 02, 2008 by larrykluger
Timeline_EventSourceJSON_jsDate  
Benefits of using js Date in JSON eventsource

Introduction

Using the js Date() object in a json event source offers substantial performance improvements over XML event source or json using iso8601 strings for the dates.

{'start': new Date(1216,2,15),
'end': new Date(1216,2,18)
}

Notes


Comment by TeamBavaria, Nov 13, 2008

To me it only worked without the 'dateTimeFormat': 'iso8601' declaration!

Comment by stewarthaines, Dec 18, 2008

I've added a new dateTimeFormat called 'javascriptnative' so I can send Date() objects in valid JSON.

I create new parser in timeline_ajax/scripts/date-time.js;

SimileAjax.DateTime.parseNativeDate = function (string) {
    try {
        var date = eval('new ' + string); // string should be 'Date(yy,mm,dd)' or another valid Date constructor
        if (date == undefined) return null;
        return date;
    } catch (e) {
        return null;
    }
};

Then in timeline_ajax/scripts/units.js define the parser function which just calls eval('new ' + string) to construct a new Date object;

SimileAjax.NativeDateUnit.getParser = function(format) {
    if (typeof format == "string") {
        format = format.toLowerCase();
    }
    if (format == "javascriptnative") {
      return SimileAjax.DateTime.parseNativeDate;
    }
    return (format == "iso8601" || format == "iso 8601") ?
        SimileAjax.DateTime.parseIso8601DateTime : 
        SimileAjax.DateTime.parseGregorianDateTime;
};

I'm writing a django app and using django.utils.simplejson encoder like this in my view;

from django.utils.simplejson import JSONEncoder
def list_items(request):
  e_dict = {}
  e_dict['dateTimeFormat'] = 'javascriptnative'
  e_list = list()
  e_dict['events'] = events
  e1 = {}
  e1['start'] = 'Date(%s)' % datetime.now().strftime('%Y,%m-1,%d,%H,%M,%S')
  e1['title'] = 'Hey now!'
  e_list.append(e1)
  return HttpResponse(JSONEncoder().encode(e_dict), mimetype='application/javascript')

Note that the javascript Date() constructor expects the month to be zero-indexed, so I subtract 1 from the python datetime '%m' argument.

Comment by compkarori, Jan 07, 2009

Regardless of how you enter the dates, the display in the popups will always be converted to display in GMT.

Comment by yuyoteam, Apr 04, 2009

not work for me, using 'dateTimeFormat': 'iso8601' -> 'start': new Date(1216,2,15) error => v1 is null, n1=v1.getTime();

Comment by ccurbaine, Dec 08, 2009

The timeline doesn't work on my website using precise dates strings or Date() objects. Only years are accepted, probably because i'm using MooTools?. Timeline is using jQuery, then I won't use it anymore... too bad, I loved it !


Sign in to add a comment
Hosted by Google Code