1. 获取当前日期是第几周 :
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);
}
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;
}
var yearWeek= y.toString() + week.toString();
var yearLastWeek = y.toString() + (week-1).toString();
console.log("今年的第几周 "+ yearWeek);
console.log("上周 "+ yearLastWeek );
return yearWeek;
}
2.获取上个月 是几月份
function getLastMonth(y,m){
if(m==1){
y=y-1;
m=12;
}else if(m<10){
m="0"+(m-1);
}
var lastMonth = y.toString()+m.toString();
console.log("这是上个月"+ lastMonth);
return lastMonth;
}
3. 获取 当前日期 格式为八位 如: 20160419
function getNowDate(){
var currentMonth = new Date().getMonth()+1; //当前月上个月
var currentYear = new Date().getFullYear(); //当前年
var currentDate = new Date().getDate(); //当前日
if(currentMonth<10){
currentMonth="0"+currentMonth;
}
if(currentDate<10){
currentDate="0"+currentDate;
}
var nowday = currentYear.toString()+ currentMonth.toString()+ currentDate.toString();
return nowday;
}
4. js 获取 几天后 或者几天前的日期
//获取几天后的日期
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
if(m.length=1){
m="0"+m.toString();
}
if(d.length=1){
d="0"+d.toString();
}
return y+m+d;
}
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);
}
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;
}
var yearWeek= y.toString() + week.toString();
var yearLastWeek = y.toString() + (week-1).toString();
console.log("今年的第几周 "+ yearWeek);
console.log("上周 "+ yearLastWeek );
return yearWeek;
}
2.获取上个月 是几月份
function getLastMonth(y,m){
if(m==1){
y=y-1;
m=12;
}else if(m<10){
m="0"+(m-1);
}
var lastMonth = y.toString()+m.toString();
console.log("这是上个月"+ lastMonth);
return lastMonth;
}
3. 获取 当前日期 格式为八位 如: 20160419
function getNowDate(){
var currentMonth = new Date().getMonth()+1; //当前月上个月
var currentYear = new Date().getFullYear(); //当前年
var currentDate = new Date().getDate(); //当前日
if(currentMonth<10){
currentMonth="0"+currentMonth;
}
if(currentDate<10){
currentDate="0"+currentDate;
}
var nowday = currentYear.toString()+ currentMonth.toString()+ currentDate.toString();
return nowday;
}
4. js 获取 几天后 或者几天前的日期
//获取几天后的日期
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
if(m.length=1){
m="0"+m.toString();
}
if(d.length=1){
d="0"+d.toString();
}
return y+m+d;
}