Angularjs: - 月日历到周历

时间:2015-01-20 06:49:38

标签: jquery angularjs calendar

我是一名新手,正在以angularjs为基础制作日历。到目前为止,我已经取得了成功,并根据月度制作了日历。

以下是我实施的代码。

  var calendarFactory = function(calMonth){
                var calendar = [],
                    monthMoment = moment().month(calMonth).startOf('month');
                _.each(_.range(moment().month(calMonth).startOf('month').day()),function(item){
                    calendar.push("");
                });

                _.each(_.range(1,moment().month(calMonth).startOf('month').daysInMonth() + 1), function(item){
                    var day = {
                        day_value:item,
                        day_name: moment().month(calMonth).startOf('month').add('days',(item - 1)).format("ddd").toUpperCase(),
                        api_date: moment().month(calMonth).startOf('month').add('days',(item - 1)).format("YYYY-MM-DD"),
                        clickable: ( calMonth > moment().month() ? true : item >= moment().format('D'))
                    };
                    calendar.push(day);
                });

                _.each(_.range(7 - (moment().month(calMonth).startOf('month').endOf('month').day() + 1)),function(item){
                    calendar.push("");
                });

                return calendar;
            };

但是现在我想每周实现相同的代码。到目前为止我对如何实现它有点困惑。我不确定如何每周转换日历代码。

有人可以帮我解决这个问题吗。

1 个答案:

答案 0 :(得分:0)

DEMO:http://jsfiddle.net/u93g87qc/2/

function GetDates(startDate, daysToAdd) {
  var aryDates = [];
  for(var i = 0; i <= daysToAdd; i++) {
   var currentDate = new Date();
   currentDate.setDate(startDate.getDate() + i);
   aryDates.push(DayAsString(currentDate.getDay()) + ", " + currentDate.getDate() + " " + MonthAsString(currentDate.getMonth()) + " " + currentDate.getFullYear());
  }

  return aryDates;
 }

 function MonthAsString(monthIndex) {
  var d=new Date();
  var month=new Array();
  month[0]="Jan";
  month[1]="Feb";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="Aug";
  month[8]="Sep";
  month[9]="Oct";
  month[10]="Nov";
  month[11]="Dec";

  return month[monthIndex];
 }

 function DayAsString(dayIndex) {
  var weekdays = new Array(7);
  weekdays[0] = "Sun";
  weekdays[1] = "Mon";
  weekdays[2] = "Tue";
  weekdays[3] = "Wed";
  weekdays[4] = "Thu";
  weekdays[5] = "Fri";
  weekdays[6] = "Sat";

  return weekdays[dayIndex];
 }

 var startDate = new Date();
 var aryDates = GetDates(startDate, 7);
 console.log(aryDates);