如何获取月份视图的第一个日期和最后日期

时间:2015-07-21 22:01:13

标签: jquery date

首先,这个问题看似重复但不是!

我试图在日历中找到给定月份的第一个和最后一个日期。(以附图黄色突出显示)

enter image description here

示例:附图中

输入:7月份

结果:

     First Date:28th June 2015

     Last Date: 8th August 2015

我试过下面的代码

   var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
       months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
         Date.prototype.getMonthName = function() {
             return months[this.getMonth()];
           };
         Date.prototype.getDayName = function() {
             return days[this.getDay()];
           };
         Date.prototype.getPreviousDate = function(beforeDays) {
           if (!days) { beforeDays = 0 }
               return new Date(new Date().setDate(this.getDate() -  beforeDays));
           }

      var date = new Date();
      var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
      var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
      var startDate = firstDay.getPreviousDate(3);//hard coded
      alert(startDate) // //This gives 28th June 2015

jsfiddle 它有效,但在这里我有硬编码值“3”,我想动态地得到它。

非常感谢帮助。

解决方案

           GetfirstVisibleDay: function(date){
               var firstDay = 0;
               var firstVisibleDay = new Date(date.getFullYear(), date.getMonth(), 0, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());

               while (firstVisibleDay.getDay() != firstDay) {
                   kendo.date.setTime(firstVisibleDay, -1 * 86400000);
               }
               return firstVisibleDay;
           },

0 个答案:

没有答案