[英]Any built-in method to get boundary dates for a date in day, week and month view in a calendar?
我需要基於當前視圖是Day或Week或Month將選定日期(來自日期選擇器)的邊界日期發送到服務器。 示例如下所示
例如:如果當天是2016年1月25日,
1. In "Day" View I need to send :
fromDate: "25012016",
toDate:"25012016"
2. "Week" view(consider Monday as first day of the week as the calendar view in my screen showing the same) :
fromDate: "25012016",
toDate:"31012016"
3."Month" view:
fromDate: "01012016",
toDate:"31012016"
不知道任何內置功能,但您可以輕松獲得
var currentDate = new Date();
var dayOfWeek = currentDate.getDay();
周
//var currentDate = new Date();
var currentDate = new Date(2016,0,19);
var dayOfWeek = currentDate.getDay();
console.log( dayOfWeek );
dayOfWeek = (dayOfWeek + 6 )%7; //calibrate it for Monday to Sunday
currentDate.setDate( currentDate.getDate() + (-dayOfWeek ) ); //first day of week accomodating +1 for making Monday as first day
document.body.innerHTML += "<br>" + currentDate.toString();
currentDate.setDate( currentDate.getDate() + 7 ); //last day of week
document.body.innerHTML += "<br>" + currentDate.toString();
月
currentDate = new Date();
currentDate.setDate( 1 ); //first day of current month
document.body.innerHTML += "<br>" + currentDate.toString();
currentDate.setMonth( currentDate.getMonth() + 1 ); //first day of next month
currentDate.setDate( currentDate.getDate() + (-1) ); //last day of current month
document.body.innerHTML += "<br>" + currentDate.toString();
謝謝大家的幫助!
使用Momentjs,我完成了工作。
// type can be "Day" or "Week" or "Month"
var getBoundaryDates = function(selecteddate, type){
var fromd = moment(selecteddate).startOf(type).format('YYYY-MM-DD');
var tod = moment(selectddate).endOf(type).format('YYYY-MM-DD');
if(type == 'Week') {
var dayToSubtract = selecteddate.getDay() == 0 ? 7 : selecteddate.getDay();
fromd = moment(new Date(selecteddate).setDate((selecteddate.getDate() - dayToSubtract) +1)).format('YYYY-MM-DD');
tod = moment(new Date(selecteddate).setDate((selecteddate.getDate() - dayToSubtract) +7)).format('YYYY-MM-DD');
}
return {
fromd : fromd,
tod : tod
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.