/    Sign up×
Community /Pin to ProfileBookmark

Problem with Date methods on json array

Hello,

I’m getting the following error message: “Object doesn’t support this property or method”, on lines 14, 15, 16 of the following code:

1 var datearray = [];
2 var jsonarray = [];
3 jsonarray = [{“twitter_id”:null,”published”:”2009-11-03 00:34:40″},
4 {“twitter_id”:null,”published”:”2009-12-01 20:48:49″},
5 {“twitter_id”:null,”published”:”2009-12-01 19:31:36″},
6 {“twitter_id”:null,”published”:”2009-11-28 14:05:09″},
7 {“twitter_id”:null,”published”:”2009-11-26 18:02:08″},
8 {“twitter_id”:null,”published”:”2009-11-25 22:14:08″},
9 {“twitter_id”:null,”published”:”2009-11-25 22:10:21″}];
10 alert(jsonarray.length);
11
12 for (var i=0; i < jsonarray.length; ++i) {
13 var datapoint = jsonarray[i].published;
14 var year = datapoint.getFullYear();
15 var month = datapoint.getMonth();
16 var day = datapoint.getDate();
17 datearray[i] = datapoint.UTC(year, month, day);
18 }

I guess the datapoint variable is invalid, but I don’t understand why. Any help would be appreciated.

Thanks very much.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameDec 03.2009 — I guess the datapoint variable is invalid, but I don't understand why.[/quote]
Well, technically it is a valid variable, just the wrong type for the way you are trying to use it -as a Date object which has methods getFullYear etc.. etc... Your datapoint variable is a string which you will either want to convert to a Date object if you actually need the year month and day variables for some other reason, or can just bypass them variables alltogether if all you need is a UTC timestamp. We can pull the numbers out of the string, and then feed them to Date.UTC -which is a 'static' method of the Date constructor. Example:
var datearray = [];
var jsonarray = [
{"twitter_id":null,"published":"2009-11-03 00:34:40"},
{"twitter_id":null,"published":"2009-12-01 20:48:49"},
{"twitter_id":null,"published":"2009-12-01 19:31:36"},
{"twitter_id":null,"published":"2009-11-28 14:05:09"},
{"twitter_id":null,"published":"2009-11-26 18:02:08"},
{"twitter_id":null,"published":"2009-11-25 22:14:08"},
{"twitter_id":null,"published":"2009-11-25 22:10:21"}
];
for (var i = 0; i &lt; jsonarray.length; ++i) {
var datapoint = jsonarray[i].published;
var strNums = datapoint.match(/(?!0d+)d+/g); //excludes leading zero, don't need for UTC
//remove strNums[3, 4, and 5] below if you only wanted year month and day,
//note that strNums[1] needs 1 subtracted from it, month needs to be 0 to 11
datearray[i] = Date.UTC(strNums[0], strNums[1] - 1, strNums[2], strNums[3], strNums[4], strNums[5]);
}
alert(datearray.join('n')); //datearray now contains all the UTC timestamps
Copy linkTweet thisAlerts:
@KorDec 03.2009 — Maybe you could simplify the code if you will write the date into a standard way, so that it can be used as a valid argument for the Date() object:

<i>
</i>var jsonarray = [
{"twitter_id":null,"published":"2009[COLOR="Blue"]/[/COLOR]11[COLOR="Blue"]/[/COLOR]03 00:34:40"},
...
]
...
for (var i=0; i &lt; jsonarray.length; ++i) {
var datapoint = [COLOR="Blue"]new Date([/COLOR]jsonarray[i].published[COLOR="Blue"])[/COLOR];
... //rest of the code
Copy linkTweet thisAlerts:
@rinascimentomanauthorDec 03.2009 — Appreciate the quick response!
Copy linkTweet thisAlerts:
@rinascimentomanauthorDec 03.2009 — The data comes back from an AJAX call, so it's already formatted that way. I suppose I could cycle through the array and add the hyphens... Thanks for responding to my post!
Copy linkTweet thisAlerts:
@KorDec 03.2009 — no problem. Use this RegExp replace:
<i>
</i>var datapoint = new Date(jsonarray[i].published.replace(/-/g,'/'));
×

Success!

Help @rinascimentoman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...