计算一年中第几周
一年中多少周
初始這個問題,計算方法很簡單,羅列如下:
w = days/7 + 1
就這么簡單,剩下的就是怎么求天數了
C++
在C++標準里面struct tm 規定tm_yday就是一年中多少天數
int NumOfWeek()
{time_t t = time(0);tm* lt = localtime(&t);return (lt->tm_yday +1) / 7 + 1;
}
Java
使用Calendar類直接可以得到這個值
Calendar cal = Calendar.getInstance();
//獲得當前日期屬于今年的第幾周
int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
Javascript
/**
* 判斷年份是否為潤年
*
* @param {Number} year
*/
function isLeapYear(year) {return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
/**
* 獲取某一年份的某一月份的天數
*
* @param {Number} year
* @param {Number} month
*/
function getMonthDays(year, month) {
return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}
/**
* 獲取某年的某天是第幾周
* @param {Number} y
* @param {Number} m
* @param {Number} d
* @returns {Number}
*/
function getWeekNumber(y, m, d) {
var now = new Date(y, m - 1, d),
year = now.getFullYear(),
month = now.getMonth(),
days = now.getDate();
//那一天是那一年中的第多少天
for (var i = 0; i < month; i++) {
days += getMonthDays(year, i);
}//那一年第一天是星期幾
var yearFirstDay = new Date(year, 0, 1).getDay() || 7;var week = null;
if (yearFirstDay == 1) {
week = Math.ceil(days / yearFirstDay);
} else {
days -= (7 - yearFirstDay + 1);
week = Math.ceil(days / 7) + 1;
}return week;
}
var lot;
function loaddate(){
var now = new Date();
var str = now.getFullYear()+"-"+((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"-"+(now.getDate()<10?"0":"")+now.getDate();
var year = now.getFullYear();
var month =((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1);
var date=(now.getDate()<10?"0":"")+now.getDate();
document.getElementById("RFLOT_DATE").value=str;
var ttwo=year.toString().substring(2,year.length);
lot=ttwo+month+date;
//document.getElementById("RFLOT_LOT").value=getWeekNumber(year,month,date)+ttwo;}
總結
- 上一篇: 送给2020年高考的考生
- 下一篇: /etc/profile ,/etc/b