简体   繁体   English

解析日期选择器的日期以显示事件日期

[英]Parse date for datepicker to show event dates

Ok so what i am trying to achieve is parse numbers of dates into datepicker and then show it. 好的,所以我想要实现的是将日期数量解析为datepicker,然后显示出来。 Simple . 很简单。 I am able to do it for chrome but in firefox i am having some issue . 我能够为Chrome做到这一点,但是在Firefox中,我遇到了一些问题。 dates are coming from ajax like this . 日期来自像这样的ajax。

     var eventDates ={};
     //lets take some demo entry 
     var dates = [{datename:'2015-04-15 10:39:57'},{datename:'2015-04-18 10:39:57'}]  

     jQuery.each(dates ,function(key,val){
       eventDates[new Date(moment(val.datename).format('MM-DD-YYYY'))] = new Date(moment(val.datename).format('MM-DD-YYYY'));
      })
     console.log(eventDates);
     jQuery('#calendar').datepicker({
         beforeShowDay: function(date) {
            var highlight = eventDates[date];
            console.log(highlight);
            if (highlight) {
                 return [true, "event", highlight];
            } else {
                 return [true, '', ''];
            }
         },
        onSelect: function(dateText, inst) { 
            location.href = '/schedule';
        }

    });

So above solution works perfectly for chrome but when it comes to firefox undefined date error get throwed. 因此,上述解决方案非常适合chrome,但涉及到Firefox时,会抛出不确定的日期错误。

I have tried some ways to parse date for firefox , which works well in chrome . 我尝试了一些方法来解析firefox的日期,该日期在chrome中效果很好。

//method 1
eventDates[Date.parse(moment(val.date).format('MM-DD-YYYY'))] = Date.parse(moment(val.date).format('MM-DD-YYYY'));

//method 2 
eventDates[moment(val.date)] =moment(val.date);

for second method dates are coming for firefox but don't get showed in datepicker. 对于第二种方法,Firefox的日期即将到来,但不会在datepicker中显示。 But it works for chrome. 但它适用于Chrome。 Ugh . gh。 Any suggestion for firefox. 任何有关Firefox的建议。

Any help ? 有什么帮助吗? Tell me if i am lost somewhere . 告诉我我是否迷路了。

Update 更新

So take from this is , datepicker has it's own method for parsing dates. 因此,datepicker具有它自己的解析日期的方法。 I have shown some strange object strings in above example , But take from this would be use datepicker parse method. 我在上面的示例中显示了一些奇怪的对象字符串,但是从中可以使用datepicker parse方法。

You're creating an object with date objects as keys and values, a really strange object, but comparing date objects that way should work in theory as toValue is called on them, but it's still a little strange. 您正在创建一个使用日期对象作为键和值的对象,这是一个非常奇怪的对象,但是在理论上比较日期对象应该是toValue ,因为在它们上调用了toValue ,但仍然有些奇怪。

Also, jQuery's datepicker has a built in way to parse dates that does basically the same as Moment. 而且,jQuery的datepicker具有一种内置的解析日期的方式,该方式与Moment基本相同。

Rewritten it would look like this instead 重写它看起来像这样

var dates = [{
    'date': '2015-04-15 10:39:57'
}, {
    'date': '2015-04-18 10:39:57'
}]

var eventDates = jQuery.map(dates, function (val) {
    return $.datepicker.parseDate('yy-mm-dd', val.date.split(' ').shift()).getTime();
});

jQuery('#calendar').datepicker({
    beforeShowDay: function (date) {
        return eventDates.indexOf(date.getTime()) != -1 ? [true, "event", date] : [true, '', '']
    },
    onSelect: function (dateText, inst) {
        location.href = '/schedule';
    }
});

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM