常用date函数

//在Jquery里格式化Date日期时间数据
function timeStamp2String(time){
var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
return year + "-" + month + "-" + date;
// var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
// var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
// var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
// return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}
//获取当前时间的前后几天
function GetDateStr(AddDayCount) {
var dd = new 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;
}
//获取时间范围内的所有时间
Date.prototype.format = function() {
var s = '';
var mouth = (this.getMonth() + 1)>=10?(this.getMonth() + 1):('0'+(this.getMonth() + 1));
var day = this.getDate()>=10?this.getDate():('0'+this.getDate());
s += this.getFullYear() + '-'; // 获取年份。
s += mouth + "-"; // 获取月份。
s += day; // 获取日。
return (s); // 返回日期。
};

function getAll(begin, end) {
var arr = [];
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;) {
//console.log((new Date(parseInt(k))).format());
k = k + 24 * 60 * 60 * 1000;
arr.push((new Date(parseInt(k))).format());
}
return arr;
}
//获取当前日期的前一个月日期
function getLastMonth(){
var latayesr = new Date().getFullYear()-1;
var currentYear = new Date().getFullYear();
var currentMonth = new Date().getMonth();
if(currentMonth<10){
currentMonth = '0'+currentMonth;
}
var date = currentYear+'-'+currentMonth+'-01';
if(currentMonth == 0){
currentYear = currentYear-1;
date =currentYear +'-'+12+'-01';
}
return date;
}

转载于:https://www.cnblogs.com/JiaFanFan/p/11231118.html

你可能感兴趣的:(常用date函数)