My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Timeline_EventSourceJSON_jsDate  
Benefits of using js Date in JSON eventsource
Updated Feb 4, 2010 by larryklu...@gmail.com

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

  • Using Date() objects obviates the need for an explicit 'dateTimeFormat': 'iso8601', declaration at the top of the file.
  • The JSON standard does not allow object declarations. So your event data set will no longer be valid JSON. But it will work fine with Timeline.
  • The Date object will use the timezone of the browser. To use a specific GMT time, use new Date(Date.UTC(2008,0,17,20,00,00,0))
  • the js Date() object takes seven arguments, first three are required:
    • Date(year, month, day, hours, minutes, seconds, milliseconds)
    • Date and Date.UTC use zero-indexed months: 0 => Jan, 1 => Feb, ..., 11 => Dec

Comment by TeamBava...@gmail.com, Nov 13, 2008

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

Comment by stewarth...@gmail.com, 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 compkar...@gmail.com, Jan 7, 2009

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

Comment by yuyot...@gmail.com, Apr 4, 2009

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

Comment by ccurba...@gmail.com, Dec 8, 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 !

Comment by pbenap...@gmail.com, Apr 26, 2010

ccurbaine: you have to enter the dates in iso8601 format, that's something like '2010-12-31' more info about the standard here -> http://es.wikipedia.org/wiki/ISO_8601

Comment by batman08...@gmail.com, Feb 14, 2012

I can't get dates before 9999 BC to display. I've used the date constructor like startdate: new Date(-13000,0,1), and that doesn't work. I've used not-quite-ISO8601 syntax (dates w/ more than 4 digits for the year are not clearly specified in the ISO8601 standard) of startdate: "-13000". When I use them it says that they cannot be plotted.


Sign in to add a comment
Powered by Google Project Hosting