获得月份功能不适用于iPad

时间:2015-06-08 09:40:49

标签: javascript ios ipad

我需要在iPad上显示数月和数天,但问题在于:

  • 在显示月份时,会显示undefined;
  • 在显示日期时,会显示nullnan

Javascript代码

  for (var x = 0; x < parsed.length; x++) {
                    var objs = parsed[x];
                    var id = objs.id;
                    var eventname = objs.abbr_name;
                    var date = objs.startDate;
                    var event_url = objs.event_url;
                    var city = objs.city;
                    var country = objs.country;
                    var time = objs.endDate;
                    //var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
                    var dateObj = new Date(date);
                    var months = dateObj.getUTCMonth() + 1;
                    var month = monthNames[dateObj.getMonth()];
                    var day = dateObj.getUTCDate();
                    console.log(month);
                    console.log(day);
                    var eventData = [];
                    if (localStorage.getItem('eventData') === null) {
                        eventData = [];
                    } else {
                        eventData = JSON.parse(localStorage.getItem('eventData'));
                    }
                    var details = {};
                    details.id = id;
                    details.eventname = eventname;
                    details.date = date;
                    details.event_url = event_url;
                    details.city = city;
                    details.country = country;
                    details.month = month;
                    details.day = day;
                    details.time = time;
                    eventData.push(details);
                    var jsondata = localStorage.setItem('eventData', JSON.stringify(eventData));
                    if (x == 0) {
                        var htmlcontent = '<a href="/' + event_url + '"><div class="row_color"><div class="col-xss-12"><p class="small1">Similar Events from your industry in ' + country + '</p> </div><div class="col-xss-1"><div class="circle_image">' + month + ' <br><span class="black_col">' + day + '</span></div></div> <div class="col-xss-8"><p>' + eventname + '</p><div style="padding-left:0px!important;" class="col-xss-8"><div class=""><p class="small22"><span class="orange_col"><i class="fa fa-map-marker icon_rightpadding"></i></span>' + city + ', ' + country + '</p></div></div></div><div class="col-xss-2"><div class="row"><span class=" btn_orange_small pull-right">Attend</span></div></div></div></a>';
                    } else if (x % 2 == 0) {
                        var htmlcontent = '<a href="/' + event_url + '" ><div class="row_color"><div class="col-xss-1"><div class="circle_image">' + month + ' <br><span class="black_col">' + day + '</span></div></div> <div class="col-xss-8"><p>' + eventname + '</p><div style="padding-left:0px!important;" class="col-xss-8"><div class=""><p class="small22"><span class="orange_col"><i class="fa fa-map-marker icon_rightpadding"></i></span>' + city + ', ' + country + '</p></div></div></div><div class="col-xss-2"><div class="row"><span class=" btn_orange_small pull-right">Attend</span></div></div></div></a>';
                    } else {
                        var htmlcontent = '<a href="/' + event_url + '" ><div class="row_color1"><div class="col-xss-1"><div class="circle_image">' + month + ' <br><span class="black_col">' + day + '</span></div></div> <div class="col-xss-8"><p>' + eventname + '</p><div style="padding-left:0px!important;" class="col-xss-8"><div class=""><p class="small22"><span class="orange_col"><i class="fa fa-map-marker icon_rightpadding"></i></span>' + city + ', ' + country + '</p></div></div></div><div class="col-xss-2"><div class="row"><span class=" btn_orange_small pull-right">Attend</span></div></div></div></a>';
                    }
                    $("#notify_bar").append(htmlcontent);
                }

以下是我对json数据所做的事情:

  • 我已将所有json保存在localstorage中。
  • localStorage.eventData:

         "[{"id":210195,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-17","event_url":"aadi-discount-shopping-fair","city":"Chennai","country":"India","month":"Jul","day":17,"time":"2015-07-20"},{"id":210198,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-24","event_url":"aadi-discount-shopping-fair-coimbatore","city":"Coimbatore","country":"India","month":"Jul","day":24,"time":"2015-07-27"},{"id":210215,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-31","event_url":"aadi-discount-shopping-fair-pondicherry","city":"Pondicherry","country":"India","month":"Jul","day":31,"time":"2015-08-03"}]"
    

虽然它在所有桌面浏览器中都没有问题,但我无法挖掘iPad的问题。

欢迎任何建议。

2 个答案:

答案 0 :(得分:1)

错误来自var date = objs.startDate;

你的json中没有startDate这样的键。即使使用Chrome桌面,我也会在控制台中获得undefinedNaN

将startDate替换为date,它可以正常工作。

parsed = [{"id":210195,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-17","event_url":"aadi-discount-shopping-fair","city":"Chennai","country":"India","month":"Jul","day":17,"time":"2015-07-20"},{"id":210198,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-24","event_url":"aadi-discount-shopping-fair-coimbatore","city":"Coimbatore","country":"India","month":"Jul","day":24,"time":"2015-07-27"},{"id":210215,"eventname":"Aadi Discount Shopping Fair","date":"2015-07-31","event_url":"aadi-discount-shopping-fair-pondicherry","city":"Pondicherry","country":"India","month":"Jul","day":31,"time":"2015-08-03"}]

var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

for (var x = 0; x < parsed.length; x++) {
    var objs = parsed[x];
    var date = objs.date;
    var dateObj = new Date(date);
    var month = monthNames[dateObj.getMonth()];
    var day = dateObj.getUTCDate();
    console.log(month);
    console.log(day);
}

答案 1 :(得分:0)

据我所知,Safari不接受带连字符的dateformat。尝试使用斜杠:

"date":"2015/07/17"

问候。