const end = new Date();
const start = new Date();
最近7天:
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
上周:
let weekday:any = new Date().getDay() || 7;
let lastweek1:any = new Date(new Date().setDate(new Date().getDate() - weekday - 6))
let lastweek2:any = new Date(new Date().setDate(new Date().getDate() - weekday))
格式转换:
const fmt=(lastweek1:any)=>{
let year:any = lastweek1.getFullYear()
let month:any =lastweek1.getMonth()+1
let day:any=lastweek1.getDate();
if(month<10){
month='0'+month
}
if(day<10){
day='0'+day
}
let start=year+'-'+month+'-'+day;
return start;
}
第一季度:
const year = new Date().getFullYear();
const start = new Date(`${year}/01/01`);
const end = new Date(`${year}/03/31`);
上几个月:
const getDateRange=(month:number)=>{
const now=new Date();
const start=new Date(now.getFullYear(),now.getMonth()-month,1)
const end=new Date(now.getFullYear(),now.getMonth(),0) //上月最后一天
return [start,end]
}
例如:
new Date(2019,11,0)-->2019-11-30
new Date(2019,11,1)-->2019-12-01
new Date(2019,11,2)-->2019-12-02
获取星期几:
1.new Date().getDay()
2.new Date(2019,11,2).getDay()
0为周一,1为周二
在原型上添加:
当构造一个原型,所有的日期对象都会默认添加属性和方法。
Date.prototype.addDate = function ( addDays) {
var date = this.valueOf()
date = date + addDays * 24 * 60 * 60 * 1000
date = new Date(date)
return date;
}
Date.prototype.addHour = function (addHours) {
var date = this.valueOf()
date = date + addHours * 60 * 60 * 1000
date = new Date(date)
return date;
}
//获取某月第一天
Date.prototype.GetMonthFirstDay = function()
{
var firstDate = new Date(this.getTime());
firstDate.setDate(1);
return firstDate;
}
//获取某月最后一天
Date.prototype.GetMonthLastDay = function ()
{
var endDate = new Date(this.getTime());
endDate = dateAdd(new Date(endDate.getFullYear(), endDate.getMonth() + 1, 1),"day",-1);
return endDate;
}
//格式
Date.prototype.Format = function (fmt) { //author: meizz
fmt = fmt.replace("YYYY", "yyyy").replace("DD", "dd");
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;
}
//日期间隔多少天
function GetDateDiff(startDate, endDate) {
var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
var dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
return dates;
}
//计算增加日期或者时间
function dateAdd(date, strInterval, Number) { //参数分别为日期对象,增加的类型,增加的数量
var dtTmp = date;
switch (strInterval) {
case 'second':
case 's':
return new Date(Date.parse(dtTmp) + (1000 * Number));
case 'minute':
case 'n':
return new Date(Date.parse(dtTmp) + (60000 * Number));
case 'hour':
case 'h':
return new Date(Date.parse(dtTmp) + (3600000 * Number));
case 'day':
case 'd':
return new Date(Date.parse(dtTmp) + (86400000 * Number));
case 'week':
case 'w':
return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
case 'month':
case 'm':
return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'year':
case 'y':
return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
}
}
//获取月天数
function getDays (year, month) {
return new Date(year, month, 0).getDate()
}