【问题标题】:fullcalendar how to set titleFormat to m/d/yyyy - m/d/yyyyfullcalendar 如何将 titleFormat 设置为 m/d/yyyy - m/d/yyyy
【发布时间】:2017-01-11 14:44:29
【问题描述】:

如何设置 titleFormat 以便日历的标题看起来像 2017 年 1 月 9 日 - 2017 年 1 月 13 日(没有周六和周日,只有周一到周五) 目前我的日历在开始时看起来像这样,标题看起来像 1/9 – 15/2017(这不是我想要的)

  $('#calendar').fullCalendar({

            header: {
                left: 'prev title next today',
                right: ''
            },
            weekends: false,
            titleFormat: "M/D/Y",
            editable: false,
            firstDay: 1,
            disableDragging: true,
            displayEventEnd: true,
            views: {
                week: {
                    type: 'basicWeek',
                    duration: {
                        days: 7
                    },
                    columnFormat: 'dddd M/D/Y'// Format the day to show full weekday and its date
                }
            },

请帮忙。

【问题讨论】:

    标签: javascript fullcalendar title


    【解决方案1】:

    您可以使用 viewRender 属性创建自己的标题格式。

    首先,你得把titleFormat改成这样:

    titleFormat: "YYYY/MM/DD"
    

    标题现在应该是这样的:

    2018/05/27 – 2018/06/02
    

    然后你可以像这样使用 viewRender 属性重新格式化标题:

     viewRender: function (view, element) {
    
                    //reformat title date for week mode
                    if (view.type == "agendaWeek") {
    
                        //Get the start date value and reformat the date
                        var startDate = moment(view.title.split("–")[0].replace(" ", "")).format("M/D/YYYY");
    
                        //You can set the day range depending on what you need
                        //If you only want to show dates from Monday to Friday, then you can get the end date value by adding 4 days from the start date
                        var endDate = moment(startDate).add(4, "days").format("M/D/YYYY");
    
                        //Override the fullcalendar title here
                        $(".fc-center h2").text(startDate + " – " + endDate);
                    }
    
                }
    

    您可以在此处阅读第二个问题的答案: FullCalendar show only weekDays

    【讨论】:

      【解决方案2】:

      对不起,你不能。

      在内部,FullCalendar 使用其formatRange 方法来格式化标题(请参阅https://fullcalendar.io/docs/utilities/formatRange/)。这采用给定的格式,并且“智能地”(它的词不是我的)拆分格式并在两个日期之间放置一个破折号,这样您就可以获得一次指定的日期格式,但有 2 天和一个破折号。因此,您可能会显示类似“2017 年 1 月 1 日至 31 日”(titleFormat: "MMM Do, Y") 之类的内容。

      无论如何,您实际上是在尝试显示实际上是多余的信息 - 您想显示两次月份和两次年份。用户可以很高兴地看到它只显示一次的月份和年份。

      【讨论】:

      • 感谢您的解释。我想我将不得不坚持原来的格式。但是,关于我的第二个问题。我怎样才能确保范围是正确的。我让它从星期一的日期开始,但它似乎以星期天的日期而不是星期五结束
      • 这是因为您隐藏了周末并将周日作为最后一天。不幸的是,标题中的日期范围没有考虑隐藏列。实际上有一个关于此的问题:github.com/fullcalendar/fullcalendar/issues/2884。似乎仅在 2 天前添加了一个修复程序,因此它可能会纳入 FullCalendar 的后续版本,但不是现在。
      • P.S.如果答案对您的理解有所帮助(即使它不是您希望的答案!)请记得投票和/或标记为已接受的答案 - 谢谢:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多