var getDayEveryDay = function(year, index) {
var d = new Date(year, 0, 1);
while (d.getDay() != 1) {
d.setDate(d.getDate() + 1);
}
var to = new Date(year + 1, 0, 1);
var i = 1;
var arr = [];
for (var from = d; from < to;) {
if (i == index) {
arr.push(from.getFullYear() + "-" + (from.getMonth() + 1) + "-" + from.getDate());
}
var j = 6;
while (j > 0) {
from.setDate(from.getDate() + 1);
if (i == index) {
arr.push(from.getFullYear() + "-" + (from.getMonth() + 1) + "-" + from.getDate());
}
j--;
}
if (i == index) {
return arr;
}
from.setDate(from.getDate() + 1);
i++;
}
}
function getYearWeekRange(year,weekNum){
var date=null;
var year=year;
var month=null;
var day=null;
var d=null;
if(weekNum==null||weekNum==""){//如果为空,默认加载当前日期,否则加载选择的周数
date=new Date();//获取系统时间
year=date.getFullYear();//年
month=date.getMonth()+1;//月
day=date.getDate();//
var d=year+"-"+month+"-"+day;
}else{
var weekDay=getDayEveryDay(year,weekNum);
d=weekDay[0];//获取对应周数的第一天
}
console.log(d);
//获取当前日期的为今年的第几周的周数,常规的获取直接调用getWeekNumber(year, month, day);
/*由于项目需要,我这儿的周定义为周五到下周四为一周,所以我传入的日期参数得往前推4天,
*然后在调用常规的计算周数的方法
*/
//var beforeFourDay=GetDateStr(-4,d);//当前日期前推4天的日期,返回值格式为2017-01-01
//console.log("beforeFourDay"+beforeFourDay);
var yearMonthDay=d.split("-");
if(weekNum==null||weekNum==""){//如果为空,默认加载当前日期的周数以及对应范围,否未为选择的周数
weekNum=getWeekNumber(yearMonthDay[0], yearMonthDay[1], yearMonthDay[2]);//按照周五到下周四为一周,计算当前日期为今年的第几周
}else{
weekNum=weekNum;
}
//获取当前日期的为今年的第几周的周数日期范围,getDateRange("2017-01-01"),在调用常规的计算周数日期的方法返回值为["2016-12-26","2017-1-1"];
/*由于项目需要,我这儿的周定义为周五到下周四为一周,
*在调用常规的计算周数日期的方法返回值时得相应做出调整getDateRange("2017-01-01");return ["2016-12-26","2017-1-1"];
* arr[0]前推3天,arr[1]后推5天,["2016-12-30","2017-1-5"],得出2017年的1月1馹,(按照周五到下周四算一周为一周的日期范围为["2016-12-30","2017-1-5"])
*/
var weekRange=getDateRange(d);//常规的传入时间返回周的范围(周一到周天) return 格式["2016-12-26","2017-1-1"]
//weekRange[0]=GetDateStr(4, weekRange[0]);//后推4天
//weekRange[1]=GetDateStr(4, weekRange[1]);//后推4天
weekRange[0]=GetDateStr(0, weekRange[0]);//后推0天
weekRange[1]=GetDateStr(0, weekRange[1]);//后推0天
//返回当前日期为[年,周数,周的范围start,周的范围end],按照周五到下周四为一周
return [year,weekNum,weekRange[0],weekRange[1]];
}
/*
*js获取当前指定的前几天的日期,往前推4天,GetDateStr(4),后推4天GetDateStr(-4)
*/
function GetDateStr(AddDayCount,date) {
var dd = new Date(date);
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0
var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0
return y+"-"+m+"-"+d;
}
/*
*这个方法是获取周对应的日期范围(常规的一周为周一到周天为一周
* 参数datevalue如:2017-01-01)
*/
function getDateRange(datevalue){
var dateValue = datevalue;
var arr = dateValue.split("-")
//月份-1 因为月份从0开始 构造一个Date对象
var date = new Date(arr[0],arr[1]-1,arr[2]);
var dateOfWeek = date.getDay();//返回当前日期的在当前周的某一天(0~6--周日到周一)
console.log(dateOfWeek);
var dateOfWeekInt = parseInt(dateOfWeek,10);//转换为整型
if(dateOfWeekInt==0){//如果是周日
dateOfWeekInt=7;
}
var aa = 7-dateOfWeekInt;//当前于周末相差的天数
var temp2 = parseInt(arr[2],10);//按10进制转换,以免遇到08和09的时候转换成0
var sunDay = temp2+aa;//当前日期的周日的日期
var monDay = sunDay-6//当前日期的周一的日期
var startDate = new Date(arr[0],arr[1]-1,monDay);
var endDate = new Date(arr[0],arr[1]-1,sunDay);
var sm = parseInt(startDate.getMonth())+1;//月份+1 因为月份从0开始
var em = parseInt(endDate.getMonth())+1;
// alert("星期一的日期:"+startDate.getFullYear()+"-"+sm+"-"+startDate.getDate());
// alert("星期日的日期:"+endDate.getFullYear()+"-"+em+"-"+endDate.getDate());
var start = startDate.getFullYear()+"-"+sm+"-"+startDate.getDate();
var end = endDate.getFullYear()+"-"+em+"-"+endDate.getDate();
var result = new Array();
result.push(start);
result.push(end);
return result;
}
function getWeekOfYear(){
var today = new Date();
var firstDay = new Date(today.getFullYear(),0, 1);
var dayOfWeek = firstDay.getDay();
var spendDay= 1;
if (dayOfWeek !=0) {
spendDay=7-dayOfWeek+1;
}
firstDay = new Date(today.getFullYear(),0, 1+spendDay);
var d =Math.ceil((today.valueOf()- firstDay.valueOf())/ 86400000);
var result =Math.ceil(d/7);
return result;
};
function getNumOfWeeks(year) {
//设置为这一年开始日期
var startDateOfYear = new Date(year, 0, 1);
//计算这一年有多少天
var daysOfYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
var weekNum = 52;
//当年份是闰年且第一天是周日时有53周
if (startDateOfYear.getDay() == 0 || daysOfYear == 366) {
weekNum = 53;
}
return weekNum;
}
var date=null;
var year=year;
var month=null;
var day=null;
date=new Date();//获取系统时间
year=date.getFullYear();//年
month=date.getMonth()+1;//月
day=date.getDate();//
var thisd=year+"-"+month+"-"+day;
var weeks = getWeekOfYear();
console.log("当前第几周weeks:"+weeks);
var rt = getDateRange("2022-01-03");
console.log("日期范围:"+rt);
var ss = getYearWeekRange(2022,1)
console.log("指定日期信息:"+ss);
function theWeek() {
var totalDays = 0;
now = new Date();
years = now.getYear()
if (years < 1000)
years += 1900
var days = new Array(12);
days[0] = 31;
days[2] = 31;
days[3] = 30;
days[4] = 31;
days[5] = 30;
days[6] = 31;
days[7] = 31;
days[8] = 30;
days[9] = 31;
days[10] = 30;
days[11] = 31;
//判断是否为闰年,针对2月的天数进行计算
if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
days[1] = 29
} else {
days[1] = 28
}
if (now.getMonth() == 0) {
totalDays = totalDays + now.getDate();
} else {
var curMonth = now.getMonth();
for (var count = 1; count <= curMonth; count++) {
totalDays = totalDays + days[count - 1];
}
totalDays = totalDays + now.getDate();
}
//得到第几周
var week = Math.round(totalDays / 7);
return week;
}
console.log("ooo:"+theWeek());
function getDate(year) {
// 一年第一天是周几
var first = new Date(year,0,1).getDay()
// 计算一年有多少天
if((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
var allyears = 366
}else {
var allyears = 365
}
// 计算一年有多少周
var week = parseInt((allyears + first) / 7)
if(((allyears + first) % 7) != 0) {
week += 1
}
return week
}
console.log("ooo:"+getDate(2028));
console