写了一些公用方法获取自然周、传入开始和结束日期,获取中间全部日期等方法
.service('DateServices', [function () {
// 获取某年自然周的方法 (如果是当年,只返回到一月到当前月的自然周数组)
this.get = function zhou(z) {
var week_arr = [];
for (var i = 12; i > 0; i--) {
week_arr.unshift(getInfo(z, i));
}
//定义空字符串
var str_week = [];
for (var j = week_arr.length - 1; j >= 0; j--) {
for (var o = 0; o < week_arr[j].length; o++) {
str_week.unshift(week_arr[j][o]);
}
}
var option_list = [];
var nowY = new Date().getFullYear();
var nowM = new Date().getMonth() + 1;
for (var f = str_week.length - 1; f >= 0; f--) {
var first = str_week[f].split("|")[0].split('-');
if (first[1] < 10) {
first[1] = '0' + first[1]
}
if (first[2] < 10) {
first[2] = '0' + first[2]
}
var second = str_week[f].split("|")[1].split('-');
if (second[1] < 10) {
second[1] = '0' + second[1]
}
if (second[2] < 10) {
second[2] = '0' + second[2]
}
var option = first[0] + '-' + first[1] + '-' + first[2] + '至' + second[0] + '-' + second[1] + '-' + second[2];
if ((first[0] < nowY) || (first[0] == nowY && first[1] <= nowM)) {
option_list.push(option);
}
}
return option_list;
} // 获取当前周日期
this.getCurrentWeek = function () {
var now = new Date();
var nowTime = now.getTime();
var day = now.getDay();
var oneDayTime = 24 * 60 * 60 * 1000;
var MondayTime = nowTime - (day - 1) * oneDayTime;
var SundayTime = nowTime + (7 - day) * oneDayTime;
var monday = new Date(MondayTime);
var sunday = new Date(SundayTime);
return formatDate(monday) + '至' + formatDate(sunday)
} //根据开始日期和结束日期获取所有日期的方法
this.getDate = function (day1, day2) {
var getDate = function (str) {
var tempDate = new Date();
var list = str.split("-");
tempDate.setFullYear(list[0]);
tempDate.setMonth(list[1] - 1);
tempDate.setDate(list[2]);
return tempDate;
}
var date1 = getDate(day1);
var date2 = getDate(day2);
if (date1 > date2) {
var tempDate = date1;
date1 = date2;
date2 = tempDate;
}
date1.setDate(date1.getDate() + 1);
var dateArr = [];
var i = 0;
while (!(date1.getFullYear() == date2.getFullYear()
&& date1.getMonth() == date2.getMonth() && date1.getDate() == date2
.getDate())) {
var dayStr = date1.getDate().toString();
var monthStr = (date1.getMonth() + 1).toString()
if (dayStr.length == 1) {
dayStr = "0" + dayStr;
}
if (monthStr.length == 1) {
monthStr = "0" + monthStr;
}
dateArr[i] = date1.getFullYear() + "-" + monthStr + "-"
+ dayStr;
i++;
/*
* document.write("<div style='display:block'>" + date1.getFullYear() +
* "-" + (date1.getMonth() + 1) + "-" + date1.getDate() + "</div>");
*/
// document.write(dateArr[i] + "<br>");
date1.setDate(date1.getDate() + 1);
}
dateArr.splice(0, 0, day1)
dateArr.push(day2);
return dateArr;
}
// 获取最近12个月数组
this.getRecentlyMonth = function () {//获取近12月日期
var dataArr = [];
var data = new Date();
var year = data.getFullYear();
data.setMonth(data.getMonth() + 1)//获取到当前月份,设置月份
for (var i = 0; i < 12; i++) {
data.setMonth(data.getMonth() - 1);//每次循环一次 月份值减1
var month = data.getMonth() + 1;
if (month < 10) {
month = '0' + month;
}
dataArr.push(data.getFullYear() + "-" + month)
}
return dataArr.reverse();
}; // 获取某年某月最后一天
this.getDaysInOneMonth = function(year, month) {
month = parseInt(month, 10);
var d = new Date(year, month, 0);
return d.getDate();
}; function getInfo(year, month) {
var d = new Date();
// what day is first day
d.setFullYear(year, month - 1, 1);
var w1 = d.getDay();
if (w1 == 0) w1 = 7;
// total day of month
d.setFullYear(year, month, 0);
var dd = d.getDate();
// first Monday
var d1;
if (w1 != 1) d1 = 7 - w1 + 2;
else d1 = 1;
var week_count;
week_count = Math.ceil((dd - d1 + 1) / 7);
//document.write(year + "年" + month + "月有" + week_count +"周");
var zhou_arr = [];
for (var i = 0; i < week_count; i++) {
var monday = d1 + i * 7;
var sunday = monday + 6;
var from = year + "-" + month + "-" + monday;
var to;
if (sunday <= dd) {
to = year + "-" + month + "-" + sunday;
} else {
d.setFullYear(year, month - 1, sunday);
to = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
}
zhou_arr.unshift(from + "|" + to);
//document.write("第"+(i+1)+"周 从" + from + " 到 " + to + "");
}
return zhou_arr;
} function YMDinfo(ymd) {
var arr = new Array();
var arr = ymd.split("|"); return arr[0].split("-")[1] + "月" + arr[0].split("-")[2] + "日" + "-" + arr[1].split("-")[1] + "月" + arr[1].split("-")[2] + "日";
} function formatDate(ns) {
var d = new Date(ns);
var month = d.getMonth() + 1;
var date = d.getDate();
if (month < 10) {
month = '0' + month;
}
if (date < 10) {
date = '0' + date;
}
var dformat = [d.getFullYear(), month, date].join('-')
return dformat;
} }])