获取过去一年12月份
function getMonths() {
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 m = data.getMonth() + 1;
m = m < 10 ? "0" + m : m;
dataArr.push(data.getFullYear() + "年" + m+"月");
}
return dataArr;
}
console.log(getMonths());
昨天日期
function getYeasterDay(){
var time=(new Date).getTime()-24*60*60*1000;
var yesterday=new Date(time);
var month=yesterday.getMonth();
var day=yesterday.getDate();
yesterday=yesterday.getFullYear() + "" + (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0" + (yesterday.getMonth() + 1)) + "" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0" + (yesterday.getDate()));
return yesterday
}
console.log(getYeasterDay());
获取当前时间节点
// 获取当前时间节点
function getCurrentDate() {
var time = new Date().format("yyyy-MM-dd hh:mm:ss");
return time;
}
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
//月份
"d+": this.getDate(),
//日
"h+": this.getHours(),
//小时
"m+": this.getMinutes(),
//分
"s+": this.getSeconds(),
//秒
"q+": Math.floor((this.getMonth() + 3) / 3),
//季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return fmt;
};
console.log('getCurrentDate'+getCurrentDate());