当前日期:
function currentTime() {
var now = new Date();
var year = now.getFullYear(); // 年
var month = now.getMonth() + 1; // 月
var day = now.getDate(); // 日
var hh = now.getHours(); // 时
var mm = now.getMinutes(); // 分
var time = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day ) : day ) + ' ' + (hh < 10 ? ('0' + hh ) : hh) + ':' + (mm< 10 ? ('0' + mm) : mm);
return time;
}
当前时间的时间戳,某日期后三天时间戳:
var currentTime = new Date().getTime();
var date = new Date(getDate);
var nextTime = Date.parse(date) + 3*24*3600*1000;
上一天、下一天
// 上一天
function lastDay(getDate) {
var curDate = new Date(getDate);
var preDate = new Date(curDate.getTime() - 24 * 60 * 60 * 1000); // 前一天
preDate = preDate.getFullYear() + '-' + (preDate.getMonth() < 9 ? ('0' + (preDate.getMonth() + 1)) : (preDate.getMonth() + 1)) + '-' + (preDate.getDate() < 10 ? ('0' + preDate.getDate()) : preDate.getDate());
return preDate;
}
// 下一天
function nextDay(getDate) {
var curDate = new Date(getDate);
var nextDate = new Date(curDate.getTime() + 24 * 60 * 60 * 1000); // 前一天
nextDate = nextDate.getFullYear() + '-' + (nextDate.getMonth() < 9 ? ('0' + (nextDate.getMonth() + 1)) : (nextDate.getMonth() + 1)) + '-' + (nextDate.getDate() < 10 ? ('0' + nextDate.getDate()) : nextDate.getDate());
return nextDate;
}
上一周、下一周:
// 当前日期,向前或向后的天数
function addDate(date, n) {
date = new Date(date);
date.setDate(date.getDate() + n);
var dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
return dateStr;
}
// 上一周
function lastWeek(getDate) {
var sendDay = addDate(getDate, -7);
return sendDay;
};
// 下一周
function nextWeek(getDate) {
var sendDay = addDate(getDate, 7);
return sendDay;
}
上一个月、下一个月:
// 上一个月
function lastMonth(getMonth) {
var date = new Date(getMonth);
var currentMonth = date.getMonth() <= 0 ? '12' : date.getMonth() - 1;
var currentYear = date.getMonth() <= 0 ? date.getFullYear() - 1 : date.getFullYear();
var lastDate = currentYear + '-' + (currentMonth < 9 ? '0' + (currentMonth + 1) : (currentMonth + 1));
return lastDate;
}
// 下一个月
function nextMonth(getMonth) {
var date = new Date(getMonth);
var currentMonth = date.getMonth() + 1 > 12 ? '1' : date.getMonth() + 1;
var currentYear = date.getMonth() + 1 > 12 ? date.getFullYear() + 1 : date.getFullYear();
var nextDate = currentYear + '-' + (currentMonth < 9 ? '0' + (currentMonth + 1) : (currentMonth + 1));
return nextDate;
}
日期格式化:
function timeFormat(date) {
date = new Date(date);
var y = date.getFullYear();
var m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1) ) : (date.getMonth() + 1); // 月
var d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate(); // 日
return y + "-" + m + "-" + d;
}
当前日期的本周第一天、最后一天:
// 第一天
function getFirstDayOfWeek(date) {
date = new Date(date);
var weekday = date.getDay() || 7; // 获取星期几,getDay()返回值是0(周日)到6(周六)之间的一个整数。0 || 7位7,即weekday的值为1-7
date.setDate(date.getDate() - weekday + 1); // 往前算(weekday-1)天,年份、月份会自动变化
return timeFormat(date);
}
// 最后一天
function getLastDayOfWeek(date) {
date = new Date(date);
var weekday = date.getDay() || 7;
date.setDate(date.getDate() - weekday + 7);
return timeFormat(date);
}
两个日期内的所有日期
function getAllDate(begin, end, excludeWeekend) { // 开始日期,结束日期,是否排除周末
var arr = [];
if(begin !== "" && end !== "") {
var ab = begin.split('-');
var ae = end.split('-');
var db = new Date();
db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
var de = new Date();
de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
for(var k = unixDb; k <= unixDe; ) {
k = k + 24 * 60 * 60 * 1000;
var newdate = new Date(parseInt(k));
var year = newdate.getFullYear();
var month = (newdate.getMonth() + 1) < 10 ? "0" + (newdate.getMonth() + 1) : (newdate.getMonth() + 1);
var day = newdate.getDate() < 10 ? "0" + newdate.getDate() : newdate.getDate();
// 判断是否包含周末
if(excludeWeekend) {
if(newdate.getDay() !== 0 && newdate.getDay() !== 6) {
arr.push(year + "-" + month + "-" + day);
}
} else {
arr.push(year + "-" + month + "-" + day);
}
}
}
return arr;
}