gpt4 book ai didi

javascript - 使用周数跳转到/显示日历中的特定周

转载 作者:行者123 更新时间:2023-11-28 00:57:34 24 4
gpt4 key购买 nike

是否有可能通过使用周数跳转到/显示特定周?例如,如果我想显示第 43 周,有什么办法可以做到这一点吗?

我知道如何使用 goToDate 选项,然后将 View 更改为周,但是是否有可能使用周数来做到这一点?

最佳答案

我在How to convert a week number to a date in Javascript找到了相关功能。有了它,只需传递年份和周数,它就会返回一个格式正确的日期对象:

var gotoweek = firstDayOfWeek( date.year(), date.weeks() );
$('#mycalendar').fullCalendar( 'gotoDate', gotoweek );

在以下示例中,单击任意一天将带我们进入相对周数的周 View :

// Create a date object based on year and week number
// https://stackoverflow.com/a/19375264/1287812
function firstDayOfWeek( year, week ) {
// Jan 1 of 'year'
var d = new Date(year, 0, 1),
offset = d.getTimezoneOffset();

// ISO: week 1 is the one with the year's first Thursday
// so nearest Thursday: current date + 4 - current day number
// Sunday is converted from 0 to 7
d.setDate(d.getDate() + 4 - (d.getDay() || 7));

// 7 days * (week - overlapping first week)
d.setTime(d.getTime() + 7 * 24 * 60 * 60 * 1000 * (week + (year == d.getFullYear() ? -1 : 0)));

// daylight savings fix
d.setTime(d.getTime() + (d.getTimezoneOffset() - offset) * 60 * 1000);

// back to Monday (from Thursday)
d.setDate(d.getDate() - 3);

return d;
}

$('#mycalendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month agendaWeek agendaDay'
},
weekNumbers: true,
dayClick: function(date, jsEvent, view) {
var gotoweek = firstDayOfWeek( date.year(), date.weeks() );
$('#mycalendar').fullCalendar( 'gotoDate', gotoweek );
$('#mycalendar').fullCalendar( 'changeView', 'agendaWeek' );
},
events: [{
title: 'Click me 1',
msg: 'I am clipped to the left which is annoying',
start: '2014-09-01 06:00:00',
end: '2014-09-01 08:00:00',
editable: false,
allDay: false
}, {
title: 'Click me 2',
msg: 'I am OK',
start: '2014-09-04 14:00:00',
end: '2014-09-04 15:00:00',
editable: false,
allDay: false
}]
});
#mycalendar {
margin: 30px;
height: 500px;
max-width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.1/fullcalendar.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.1.1/fullcalendar.min.css">
<div id="mycalendar"></div>

关于javascript - 使用周数跳转到/显示日历中的特定周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26063199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com