I have been working this morning on creating a web app that takes information from a JSON file - in this case, tweet information from the @POTUS account, as The White House just made a ton of information publically accessible - and presents it on the screen.
Specifically, I have been working on taking the JSON file of @POTUS tweet information, and displaying the text of the tweet, and the date. The date is taken from a timestamp, and I have used a bit of JS to translate it into a format that is appealing to me.
My problem is this: the date shows up correctly in Chrome (where I have been testing), but when viewing it in Safari, Firefox, or a mobile device, the date returns "undefined NaN, NaN", where month is returning "undefined" and day and year are both returning "NaN". Looking through my code, I cannot find a reason why this would be happening.
Here is my HTML file in it's entirety:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>@POTUS</title>
<link rel="stylesheet" href="assets/css/main.css">
<link href="https://fonts.googleapis.com/css?family=Lato:300,900" rel="stylesheet">
</head>
<body>
<div class="tweet-container">
<header>
<h1 class="title">
<a class="twitter" href="http://www.twitter.com/potus" target="_blank">@POTUS</a> Tweets
</h1>
<p class="description">A complete archieve of tweets from President Barack Obama, presented in reverse chronological order.</p>
</header>
<div id="potus-tweets"></div>
<button id="btn">New Tweet</button>
</div>
<script src="assets/js/tweets.js"></script>
</body>
</html>
Here is my JS file in it's entirety:
var tweetCounter = 0;
var tweetsContainer = document.getElementById("potus-tweets");
var btn = document.getElementById("btn");
btn.addEventListener("click", function(){
var request = new XMLHttpRequest();
request.open('GET', 'assets/js/potus.json');
request.onload = function(){
var potusTweets = JSON.parse(request.responseText);
loadTweets(potusTweets);
};
request.send();
});
function loadTweets(data){
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var date = new Date(data[tweetCounter].timestamp);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var tweet = "";
for (i = 0; i < 1; i++){
tweet += '<div class="tweet"><p class="copy">' + data[tweetCounter].text + '</p><p class="date">' + monthNames[month] + ' ' + day + ', ' + year + '</p></div>';
tweetCounter++;
};
tweetsContainer.insertAdjacentHTML('beforeend', tweet);
};
For reference, the project is live here: http://thejessicafelts.github.io/projects/POTUS-Tweets/
And the code can be found here: https://github.com/thejessicafelts/POTUS-Tweets
Any insights as to why this isn't working would be extremely appreciated. I have been trying to troubleshoot this all morning, and I cannot see anything that would be causing the return of "undefined NaN, NaN".
Thanks in advance