每个月都会发生一天的事情

时间:2015-09-29 11:59:16

标签: javascript angularjs underscore.js

我有一个月的第一天和最后一天,当前月份的长度我正在寻找一个能让我在这个月的任何一天出现的功能,我已经看到一些功能给出了下一次出现,但我想能够进入任何一天,看到所有都是事件..我想这将是某种循环..我正在努力,因为我们说,但任何帮助将是伟大的..

$scope.recurrenceFields.by_year_day=$filter('date')($scope.fields.times.dateStart, 'yyyy');
$scope.recurrenceFields.by_month=$filter('date')($scope.fields.times.dateStart, 'M');
    var correntDay = new Date();
                           var lastDayOfMonth = new Date(correntDay.getFullYear(), correntDay.getMonth()+1, 0);
                           var firstDayOfMonth = new Date(correntDay.getFullYear(), correntDay.getMonth(), 1);
                           function daysInMonth(month,year) {
                                return new Date($filter('date')($scope.fields.times.dateStart, 'yyyy'), $scope.recurrenceFields.by_month, 0).getDate();
                            }

2 个答案:

答案 0 :(得分:2)

不太确定“本月任何一天的所有出现”是什么意思,但是以下函数返回给定年,月和日数的一个月中特定日的所有出现次数。

/* @param {number} year - calendar year
** @param {number} month - calendar month: 1-Jan, 2-Feb, etc.
** @param {number} dayNumber - day number: 0-Sunday, 1-Monday, etc.
** @returns {Array} Dates for all days in month of dayNumber
*/
function getAllDaysInMonth(year, month, dayNumber) {
  var d = new Date(year, month-1, 1);
  var dates = [];
  var daysToFirst = (dayNumber + 7 - d.getDay()) % 7;
  var firstOf = new Date( d.setDate(d.getDate() + daysToFirst));

  while (firstOf.getMonth() < month) {
    dates.push(new Date(+firstOf));
    firstOf.setDate(firstOf.getDate() + 7);
  }
  return dates;
}

// Return array of all Thursdays in July 2015
console.log(getAllDaysInMonth(2015,7,4));

// [Thu 02 Jul 2015,
//  Thu 09 Jul 2015,
//  Thu 16 Jul 2015,
//  Thu 23 Jul 2015,
//  Thu 30 Jul 2015]

// Get all Tuesdays in February 2000
console.log(getAllDaysInMonth(2000,2,2));
// [Tue 01 Feb 2000,
//  Tue 08 Feb 2000,
//  Tue 15 Feb 2000,
//  Tue 22 Feb 2000,
//  Tue 29 Feb 2000]

答案 1 :(得分:0)

您可以使用此服务:

angular.module('myApp.services')
    .factory('dateUtils', function () {
        return {
            getIntervals: function (startTimestamp, endTimestamp, interval) {
                if(!angular.isNumber(startTimestamp) || !angular.isNumber(endTimestamp) || !angular.isNumber(interval) || startTimestamp===0 || endTimestamp===0) {
                    return [];
                }
                var intervals = [];
                var currentPeriod = startTimestamp;
                while (currentPeriod <= endTimestamp) {
                    intervals.push(currentPeriod);
                    var currentPeriodDate = new Date(currentPeriod);
                    currentPeriodDate.setDate(currentPeriodDate.getDate() + interval);
                    currentPeriod = currentPeriodDate.getTime();
                }
                return intervals;
            }
        };
    });

如果您想在两个日期之间全天,请使用该服务:

dateUtils.getIntervals(startDate, endDate, 1);