[英]Outputting Next Available Date from JSON Response
我正在使用Eventbrite API的JSON
响应来显示“下一个”事件巡回日期。 通过查看当前时间并找到当前时间之后的下一个事件,可以自动计算该日期。
这是我需要解析的JSON
响应:
https://jsfiddle.net/dgc223/k9dbvzoo/
如果我现在正在检查它,我希望从277行(2017-11-11T11:00:00)
返回日期。 如果要在几个小时内检查一下,我想返回350行(2017-12-09T11:00:00)
,因为那是该行程的下一个可用日期。
我拥有的当前脚本(如下)从上面的JSON
响应返回系列的第一个日期。 但是,在考虑了当前时间之后,我需要对其进行修改以返回NEXT Tour日期。 这样,脚本可以通过告诉用户下一个可用的游览日期来提供更多信息。
<script>
$.getJSON('eventbrite_api2.php', {
eventid: '35719663475',
function: 'events'
}, function(response) {
var date = new Date(response.start.utc); //this formats the date to local
time zone
date.toString();
event_start = date.toLocaleString('en-US', {
weekday: 'long',
day: 'numeric',
month: 'long',
year: 'numeric',
hour: 'numeric',
hour12: true,
timeZone: "America/New_York"
}); //this formats the date to eastern time zone, and makes some other formatting of the date
var content = "<h4>" + response.name.text + "</h4><p>The next event starts " + event_start + "</p>" + "<p>" + "<img width=\"350\" src=" + response.logo.original.url + "</img>";
$("#eventbrite").append(content);
});
</script>
我会过滤掉已经过去的所有事件,如果有事件,则使用第一个。 因此,处理响应的函数将如下所示。
function (response) {
var date = new Date();
var upcoming = response.events.filter(function (event) {
// just compare the UTC representation
var start = new Date(event.start.utc);
return start > date;
});
if (upcoming.length) {
//next event is upcoming[0]; Do what you want with it
} else {
//no events upcoming
}
}
javascript数组的filter函数接受一个test函数,并返回一个新数组,该数组仅包含第二个数组中传递给test函数时返回true的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.