Google Chart - arrayToDataTable not an array error - javascript

I have a backend in nodejs that generates an array and call a javascript function to plot a graph.
var data = google.visualization.arrayToDataTable(value)
The problem that I got a uncaught error:
Printing the variable value I have:
[ [ 'ml', 'date' ],
[ 'Sun Nov 24 2019 13:30:00 GMT-0200 (Brasilia Summer Time)',
481 ],
[ 'Sun Nov 24 2019 14:00:00 GMT-0200 (Brasilia Summer Time)',
571 ] ]
Reading similar posts like Google Chart - uncaught error: not an array i tried:
var data = google.visualization.arrayToDataTable(JSON.parse(values));
But i get the following error:
If I hardcode values it works fine:
var values = google.visualization.arrayToDataTable([
['ml','date'],
['Sun Nov 24 2019 13:30:00 GMT-0200 (Brasilia Summer Time)',481 ],
['Sun Nov 24 2019 14:00:00 GMT-0200 (Brasilia Summer Time)',571 ]
]);
var data = google.visualization.arrayToDataTable(values);
I couldn't see any difference printing the variable from hardcoding.
Any ideia?

the array is most likely being passed as a string.
if so, you need to use --> JSON.parse
but you need to swap the single quotes (') for double quotes (")
see following snippet, which works...
var test = JSON.parse('[["ml","date"],["Sun Nov 24 2019 13:30:00 GMT-0200 (Brasilia Summer Time)",481 ],["Sun Nov 24 2019 14:00:00 GMT-0200 (Brasilia Summer Time)",571 ]]');
console.log(test);
while this snippet does not...
var test = JSON.parse("[['ml','date'],['Sun Nov 24 2019 13:30:00 GMT-0200 (Brasilia Summer Time)',481 ],['Sun Nov 24 2019 14:00:00 GMT-0200 (Brasilia Summer Time)',571 ]]");
console.log(test);

Related

How to convert this date with php

How to convert this date format that is coming from javascript:
Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)
These formats are not working
$date = date('c', strtotime(Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time));
$date = date('Y-m-d h:i:s', strtotime(Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time));
The date goes back to 1969
Any insights?
may you should try skip "(Brasilia Standard Time)" in the data submit.
$date = date('Y-m-d h:i:s', strtotime("Thu Mar 04 2021 18:00:00 GMT-0300"));
var_dump($date);
I got this result:
string(19) "2021-03-04 01:00:00"
Edit: If you can 'skip' that string, you can do something like:
$strTime = "Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)";
$strTime = str_replace($strTime, ""," (Brasilia Standard Time)");
$date = date('Y-m-d h:i:s', strtotime($strTime));
var_dump($date);
output:
string(19) "1969-12-31 04:00:00"
I would suggest using the modern DateTime interface and specifying a proper format, rather than having PHP try to guess how to interpret the date string. You can also use the + specifier to ignore trailing data rather than having to edit your input in advance.
$string = 'Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)';
$date = DateTime::createFromFormat('D M d Y H:i:s T+', $string);
var_dump($date, $date->format('c'));
Output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2021-03-04 18:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "-03:00"
}
string(25) "2021-03-04T18:00:00-03:00"

Why are those dates different?

Here the console output:
new Date(2016, 08, 22)
Thu Sep 22 2016 00:00:00 GMT+0200 (CEST)
new Date("2016, 08, 22")
Mon Aug 22 2016 00:00:00 GMT+0200 (CEST)
Different months but why ?
Javascript months are 0 based in the numeric case, but in the string parsing 08 is mapped to August as it is a string translation of "August" in standard date format.
Date is being invoked in different ways as mentioned here
new Date();
new Date(value);
new Date(dateString);
new Date(year,month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
In your case, 3 & 4 formats are being called.
>> new Date(2016, 08, 22)
>> Thu Sep 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 01, 22)
>> Mon Feb 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 0, 22)
>> Fri Jan 22 2016 00:00:00 GMT+0530 (IST)
>> new Date("2016-08-22")
>> Mon Aug 22 2016 05:30:00 GMT+0530 (IST)
>> new Date("2016/08/22")
>> Mon Aug 22 2016 00:00:00 GMT+0530 (IST)
#RobG's input from the comments:
...parsing ofstrings other than ISO 8601 extended format is entirely
implementation dependent. The result of parsing "2016, 08, 22" could be anything, including an invalid Date.
The second Date constructor you use is intended to parse a (known) string representation of a date, like "Dec 25, 1995". The format you pass in is not a standard one, so even though the result is close to the correct date (and could be fixed by correcting the month value, as pointed out by DhruvPathak), it should not be used as results my differ depending on the runtime/browser.

angularjs How to remove an object from a array by underscore or anything else

My question looks familiar to other questions, but it is not!
Please take a look to understand mine condition.
I have an array of objects it looks like this
$scope.events =[
{
$$hashKey: "009"
_allDay:false
_id:5
allDay:false
className:[]
end:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
start:Date {Fri Aug 08 2015 12:30:00 GMT+0530 (IST)}
title:"Birthday Party"
},
{
$$hashKey:"006"
_id:2
end:Date {Wed Aug 05 2015 00:00:00 GMT+0530 (IST)}
start:Date {Sun Aug 02 2015 00:00:00 GMT+0530 (IST)}
title:"Long Event"
},
{
$$hashKey:"007"
_id:3
allDay:false
id:999
start:Date {Fri Aug 07 2015 13:00:00 GMT+0530 (IST)}
title:"Angular Event"
},
{
$$hashKey:"008"
_id:4
allDay:false
id:999
start:Date {Tue Aug 11 2015 16:00:00 GMT+0530 (IST)}
title:"Repeating Event"
},
{
$$hashKey:"00A"
_id:6
end:Date {Sat Aug 29 2015 00:00:00 GMT+0530 (IST)}
start:Date {Fri Aug 28 2015 00:00:00 GMT+0530 (IST)}
title:"Click for Google"
}
]
Now I have to remove a object from these array, which is look like this
var selectedObj = {
$$hashKey:"009"
_allDay:false
_id:5
allDay:false
className:[]
end:Date {Fri Aug 07 2015 12:30:00 GMT+0530 (IST)}
start:Date {Fri Aug 07 2015 12:00:00 GMT+0530 (IST)}
title:"Birthday Party"
}
What I am doing
removedArray = _.reject($scope.events, function(event) {
return event.$$hashKey == selectedObj.$$hashKey
});
$scope.events = removedArray;
$scope.events is not updated I've tried $apply but not got success.
could someone please help me to find out what I am doing wrong.
What is the best practice to do. This kind of dirty stuffs.
This should get you there using pretty standard javascript (IE9+):
var index = myArray.map(function(e) { return e.$$hashKey; }).indexOf(selectedObj.$$hashKey);
if(index != -1) {
myArray.splice(index, 1);
With underscore.js, you can do something like:
myArray = _(myArray).filter(function(obj) {
return obj.$$hashKey!== selectedObj.$$hashKey;
});
You may want to use lodash's remove function, it is basically much the same as the answer by dustmouse, but in my opinion the code is more readable.
var selectedObj = {...}
$scope.events = [0,1,...,n];
$scope.events = _.remove($scope.events, function(element) {
return element == selectedObj;
});
selected event can delete by this process. call the function deleteEvent.
$scope.deleteEvent= function() {
var index = $scope.events.indexOf(selectedObj);
$scope.events.splice(index, 1);
};

Javascript vs Date

I have some interesting problem and can't find the solution. Look at this:
var d1 = new Date("07 31 2014");
document.write(d1);
document.write('<br />');
var d2 = new Date(1406746800 * 1000);
document.write(d2);
when I run this script I get this result:
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
then after I changed my time zone I get this result:
Thu Jul 31 2014 00:00:00 GMT-0800 (AKDT)
Wed Jul 30 2014 11:00:00 GMT-0800 (AKDT)
as you can see the second result is Jul 30, but first result is Jul 31. I think they must both be equal to 31 Jul. I know this problem depends on the timezone but is there a solution?
So the constructor parameter is:
an Integer value representing the number of milliseconds since 1
January 1970 00:00:00 UTC
So given your integer value, that represents (for me, in BST):
Wed Jul 30 2014 20:00:00 GMT+0100
Which is
Wed Jul 30 2014 19:00:00 UTC
And your timezone is GMT-8, so thats the above -8 which gives:
Wed Jul 30 2014 11:00:00 GMT-0800 AKDT
The date string constructor constructs the date in your local timezone. You can see what the value should be by doing this:
new Date("07 31 2014").getTime() / 1000
Which returns 1406761200, not 1406746800

How to compare dates to see where adjacent times meet?

I have dates currently formatted in the following way:
[ [ Tue Jun 17 2014 09:00:00 GMT-0400 (EDT),
Tue Jun 17 2014 10:00:00 GMT-0400 (EDT) ] ]
[ [ Thu Jun 19 2014 09:30:00 GMT-0400 (EDT),
Thu Jun 19 2014 11:30:00 GMT-0400 (EDT) ] ]
[ [ Tue Jun 17 2014 10:00:00 GMT-0400 (EDT),
Tue Jun 17 2014 11:00:00 GMT-0400 (EDT) ] ]
These dates are actually "sessions", and I need to see where certain sessions are adjacent to each other. For example, in this specific case, the first array of dates has a end time of 10AM while the last array of dates has a start time for 10AM. How can I computationally find this situation?
The one approach I have is to first sort the array sets from earliest time to to latest time, and then compare each of the start/end date pairs to see if they are the same, but I can't seem to get it through. Any ideas are welcome!
Turn the strings into Unix timestamps with Date.parse() (if these are actually Date objects, then use the .getTime() method) and then order the sessions with Array.prototype.sort(). Here's an example where the sessions are ordered by start time:
var sessions = [
["Tue Jun 17 2014 09:00:00 GMT-0400 (EDT)", "Tue Jun 17 2014 10:00:00 GMT-0400 (EDT)"],
["Thu Jun 19 2014 09:30:00 GMT-0400 (EDT)", "Thu Jun 19 2014 11:30:00 GMT-0400 (EDT)"],
["Tue Jun 17 2014 10:00:00 GMT-0400 (EDT)", "Tue Jun 17 2014 11:00:00 GMT-0400 (EDT)"]
];
for (var i = 0; i < sessions.length; i++) {
sessions[i].startTime= Date.parse(sessions[i][0]);
}
sessions.sort(function(a, b) { return a.startTime-b.startTime; });

Categories

Resources