Javascript 从完整日历中获取点击日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28493618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:52:01  来源:igfitidea点击:

get clicked date from full calendar

javascriptjqueryfullcalendar

提问by Aneeez

Clicking a date on fullcalendarlaunches a modal '#view_event'. I want to get the date on which I have clicked. each date has the class '.fc-day' has an attribute data-date="2015-02-13". so I tried the following.

单击fullcalendar上的日期启动模态“#view_event”。我想获得我点击的日期。每个日期都有类 '.fc-day' 有一个属性 data-date="2015-02-13"。所以我尝试了以下方法。

$(".fc-day").click(function(){
    var fetchDate = $(this).data("date");
    $("#displayDate").text(fetchDate);
});

'#displayDate' is a p element in the modal. The above code isn't working. What's wrong?

'#displayDate' 是模态中的 ap 元素。上面的代码不起作用。怎么了?

回答by prime

Don't use the classesto access the date. there are ways to get the date and below is one where you can do it when you initialize the calendar , Using that you can get the date, monthand yearseparately.

不要使用classes来访问日期。有办法获取日期和下方是一个地方,当你初始化日历,你可以做到这一点,使用,你可以得到的datemonthyear分别。

    $('#calendar').fullCalendar({

        dayClick: function(date, jsEvent, view) { 
            alert('Clicked on: ' + date.getDate()+"/"+date.getMonth()+"/"+date.getFullYear());  
        },

    });

回答by jupeter

Use 'select' in fullcalendar

在全日历中使用“选择”

o.fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month'
    },
    select: 
      function(start,end){ 
          alert(start); 
          alert(end); 
          // var selDate = new Date(start);
          // add your function
    },
    editable: false
});

fullcalendar/select

全日历/选择

回答by Gautam Solanki

Fullcalendar provide default event to get selected date and time. So don't need to use any extra thing.Only you need to use below code snippet and you will get selected date in full calendar. It will be UTC format so you have to make some processing to get that date as per your timezone

Fullcalendar 提供默认事件以获取选定的日期和时间。所以不需要使用任何额外的东西。只需要使用下面的代码片段,你就会在完整的日历中获得选定的日期。它将是 UTC 格式,因此您必须进行一些处理才能根据您的时区获取该日期

$('#calendar').fullCalendar({ 
     dayClick: function(date, jsEvent, view) { 
         alert("Selected Date:"+date._d)
      }
  });

please give your feedback if you find this answer useful.

如果您发现此答案有用,请提供反馈。

回答by umarbilal

According to the official docs https://fullcalendar.io/docs/v3/handlers

根据官方文档https://fullcalendar.io/docs/v3/handlers

dayClick: function(date, jsEvent, view) {
    console.log('clicked on ' + date.format());
}