Created
August 21, 2018 20:11
-
-
Save colinfwren/b49f3afacf826e3f340659b3281c3d45 to your computer and use it in GitHub Desktop.
Script to get events from JSON and put them on page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getFormattedDate = function(distance){ | |
var days = Math.floor(distance / (1000 * 60 * 60 * 24)); | |
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | |
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | |
var seconds = Math.floor((distance % (1000 * 60)) / 1000); | |
return days + "d " + hours + "h " + minutes + "m " + seconds + "s "; | |
} | |
var container = $('#demo'); | |
$.get('https://gist.githubusercontent.com/Gimpneek/497f2bf113a3627467885de790faf40f/raw/108630fd8154e535cbe03354d775c9da826edd64/events.json', function(data){ | |
var json = JSON.parse(data); | |
var events = json.map(function(item){ | |
item.startDate = new Date(item.startDate); | |
item.endDate = new Date(item.endDate); | |
return item; | |
}).filter(function(item){ | |
return item.endDate > new Date(); | |
}); | |
events.map(function(item, ind){ | |
container.append('<div class="event"><p>' + item.name + '</p><p id="event' + ind + '"></p></div>'); | |
var interval = setInterval(function(){ | |
var el = $('#event' + ind); | |
var distance = item.endDate.getTime() - new Date().getTime(); | |
if(distance > -1){ | |
el.text(getFormattedDate(distance)); | |
} else { | |
el.parent().remove(); | |
clearInterval(interval); | |
} | |
}, 1000); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make this display one event add a new line before line 20 with
events = [events[0]]
;