Creating dates from strings is a bad idea. Each browser parsing strings a
bit differently, and thus they won't all generate the same date given the
same string. You should manually parse your strings and then build date
objects from them:
var dateStr = '2014-03-30T01:30:00+01:00';
var dateArr =
dateStr.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
var year = parseInt(dateArr[1]);
var month = parseInt(dateArr[2]) - 1; // convert month to javascript's
0-indexed months
var day = parseInt(dateArr[3]);
var hours = parseInt(dateArr[4]);
var minutes = parseInt(dateArr[5]);
var seconds = parseInt(dateArr[6]);
var date = new Date(year, month, day, hours, minutes, seconds);
Javascript does not handle timezones well, as all dates are created in the
browser's locale time (except for some browsers when creating dates from
strings). If you need to account for timezone differences, you can pull
the offset data from your date strings as well, modify the hours/minutes to
get to UTC time, and then get the browser's timezone offset and adjust them
again to change your dates to local time.
On Monday, June 2, 2014 10:26:00 AM UTC-4, Stef Coenen wrote:
>
> Hi, we are creating an web application that displays a chart of
> electricity meters data. We receive it in the form of a json array and
> parse it into regular JavaScript date objects and values. We use the Google
> visualization api for displaying this data.
>
> Everything works as expected, only when we request data from the 30th
> March we receive an "error" (as returned by Google chart). The following
> code is a simplified version where dataTable 'data' is creating the error
> and the dataTable 'datab' is not creating any errors:
>
>> function drawVisualization() {
>> // Some raw data (not necessarily accurate)
>> var data = google.visualization.arrayToDataTable([
>> ['Sequence', 'Usage'],
>> [new Date("2014-03-30T01:30:00+01:00"), 10],
>> [new Date("2014-03-30T01:45:00+01:00"), 135],
>> [new Date("2014-03-30T03:00:00+02:00"), 157],
>> [new Date("2014-03-30T03:15:00+02:00"), 139],
>> [new Date("2014-03-30T03:30:00+02:00"), 136]
>> ]);
>>
>> var datab = google.visualization.arrayToDataTable([
>> ['Sequence', 'Usage'],
>> [new Date("2014-03-30T01:30:00+01:00"), 10],
>> [new Date("2014-03-30T01:45:00+01:00"), 135]
>> ]);
>>
>> // Create and draw the visualization.
>> var ac = new
>> google.visualization.AreaChart(document.getElementById('visualization'));
>> ac.draw(data, {
>> title: 'Electricity usage',
>> isStacked: true,
>> width: 600,
>> height: 400
>> });}
>>
>> google.setOnLoadCallback(drawVisualization);
>>
>> A plnkr of this code can be found on the following link: link
> <http://plnkr.co/edit/S2H4Qx8NChgT59caIJXf?p=preview> Please keep in mind
> that I am located in UTC+1 and the error might not show in all time zones.
> Thanks
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.