获取标准时间
var nowdays = new Date();
获取当天
var days = new Date().getDate();
获取当月
var month = new Date().getMonth() + 1;
获取当年
var year = new Date().getFullYear();
获取当前月有多少天
var day = new Date(year, month, 0).getDate();
获取当前月份的所有日期
let now = new Date(days);
let current_month_num = day;
let current_month = [];
for (let i = 1; i <= current_month_num; i++) {
let day = now.setDate(i);
current_month.push(day );
}
格式日期 如2021-06-15这样的格式
formatDate(date) {
date = new Date(date);
let myyear = date.getFullYear();
let mymonth = date.getMonth() +1;
let myweekday = date.getDate();
mymonth < 10 ? mymonth = "0" + mymonth : mymonth;
myweekday < 10 ? myweekday = "0" + myweekday :myweekday;
return `${myyear}-${mymonth}-${myweekday}`;
},
获取今天到昨天的时间日期
let currentDay = new Date();
let yesterDay = new Date();
yesterDay.setDate(currentDay.getDate() - 1);
this.dateValue =[yesterDay.toJSON().slice(0,10),currentDay.toJSON().slice(0,10)];
获取上周到这周的时间
let currentDay = new Date();
let yesterDay = new Date();
yesterDay.setDate(currentDay.getDate() - 7);
this.dateValue = [yesterDay.toJSON().slice(0,10),currentDay.toJSON().slice(0,10)];
根据起始日期和结束日期获得中间所有日期
/*根据传入时间加一天 传入时间格式为yyyy-MM-dd*/
addOneDay: function(currentDay) {
let d = new Date(currentDay);
d = +d + 1000 * 60 * 60 * 24;
d = new Date(d);
return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
},
/*将字符串类型时间转换为yyyy-MM-dd格式*/
simpleDate: function(oldDate) {
let dateArr = oldDate.split("-");
dateArr["1"] = dateArr["1"].length == 1 ? "0" + dateArr["1"] : dateArr["1"];
dateArr["2"] = dateArr["2"].length == 1 ? "0" + dateArr["2"] : dateArr["2"];
return dateArr.join("-");
},
getEveryDay: function(startDate, endData) {
let dateList = [startDate];
let dayCount = (new Date(endData) - new Date(startDate)) / (1000 * 60 * 60 * 24);
for (let i = 0; i < dayCount; i++) {
let currentDay = common.addOneDay(dateList[i]);
currentDay = common.simpleDate(currentDay);
dateList.push(currentDay);
}
return dateList;
},
获取时间的默认值
getDate() {
let start = this.Fungetdate (-5)
let end = this.Fungetdate (0)
this.dateValue.push(start,end)
},
Fungetdate (count) {
var dd = new Date();
dd.setDate(dd.getDate() + count);//获取p_count天后的日期
var y = dd.getFullYear();
var m = dd.getMonth() + 1;//获取当前月份的日期
var d = dd.getDate();
return y + "-" + m + "-" + d;
}
获取当前周从星期一到星期天的日期
getDates() {
var currentDate = new Date();
var timesStamp = currentDate.getTime();
var currenDay = currentDate.getDay();
for (var i = 0; i < 7; i++) {
this.dates.push(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(/\//g, '-'));
}
},
获取当前周的日期
var now =new Date();
var nowTime =now.getTime();
var day =now.getDay();
var oneDayTime =24*60*60*1000;
//显示周一
var MondayTime =nowTime - (day-1)*oneDayTime;
//显示周日
var SundayTime = nowTime +(7-day)*oneDayTime;
//初始化日期时间
var monday =new Date(MondayTime);
var sunday = new Date(SundayTime);
this.dateValue.push(monday,sunday);
获取当天,前一天,后一天的日期
var Yestodays =simpleDateToString(new Date(new Date().getTime() - 24*60*60*1000)); //前一天
var Todays =simpleDateToString(new Date(new Date().getTime())); //当天
var Tomorrow=simpleDateToString(new Date(new Date().getTime() + 24*60*60*1000)); //后一天
/*将标准时间格式转换为yyyy-MM-dd格式*/
simpleDateToString: function(oldDate) {
return this.simpleDate(oldDate.getFullYear() + "-" + (oldDate.getMonth() + 1) + "-" + oldDate.getDate());
},
计算两个日期之间的间隔
var getDate = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
getDate (new Date("2021-11-3"), new Date("2022-2-1")) // 90
查找日期位于一年中的第几天
const dayOfYear = (date) => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
dayOfYear(new Date()); //307
获取上一个月的第一天和最后一天
var first = new Date(new Date().getFullYear(), new Date().getMonth()-1 ,1).toLocaleDateString();
var end = new Date(new Date().getFullYear(), new Date().getMonth(), 0).toLocaleDateString();