获取当前时间、获取当前月份的第一天和最后一天、获取当前年份的第一天和最后一天
1、获取当前日期
export const today = ()=> {
let today = new Date();
let nowTime = today.getTime();
today.setTime(parseInt(nowTime));
let oYear = today.getFullYear();
let oMoth = (today.getMonth() + 1).toString();
if (oMoth.length <= 1) oMoth = "0" + oMoth;
let oDay = today.getDate().toString();
if (oDay.length <= 1) oDay = "0" + oDay;
let days = today.getDay();
switch (days) {
case 1:
days = '星期一';
break;
case 2:
days = '星期二';
break;
case 3:
days = '星期三';
break;
case 4:
days = '星期四';
break;
case 5:
days = '星期五';
break;
case 6:
days = '星期六';
break;
case 0:
days = '星期日';
break;
}
let h = today.getHours().toString();
if ((h).length <= 1) h = "0" + h;
let m = today.getMinutes().toString();
if (m.length <= 1) m = "0" + m;
let s = today.getSeconds().toString();
if (s.length <= 1) s = "0" + s;
return oYear + "-" + oMoth + "-" + oDay + " " + h + ":" + m + ":" + s;
}
2、获取当前月的第一天 例如 2022-04-01 00:00:00
export const currmonthStarttime = () => {
let today = new Date();
today.setDate(1)
let oYear = today.getFullYear();
let oMoth = (today.getMonth() + 1).toString();
if (oMoth.length <= 1) oMoth = "0" + oMoth;
let oDay = today.getDate().toString();
if (oDay.length <= 1) oDay = "0" + oDay;
return oYear + "-" + oMoth + "-" + oDay + " " + '00' + ":" + '00' + ":" + '00';
}
3、获取当前年的第一天 例如 2022-01-01 00:00:00
export const curryearStarttime = () => {
var firstDay = new Date();
firstDay.setDate(1);
firstDay.setMonth(0);
firstDay = firstDay.Format("yyyy-MM-dd");
return firstDay + " " + "00:00:00"
}
4、获取当前月份的第一天和最后一天
export const getMonthFirstLastDay =()=> {
var myDate = new Date();
var currentMonth = myDate.getMonth();
var firstDay = new Date(myDate.getFullYear(), currentMonth, 1)
var lastDay = new Date(firstDay.getFullYear(), currentMonth + 1, 0);
firstDay = firstDay.Format("yyyy-MM-dd");
lastDay = lastDay.Format("yyyy-MM-dd");
return (firstDay + " " + "00:00:00") + "~" + (lastDay + " " + "23:59:59");
}
5、获取当前年份的第一天和最后一天
export const getYearFirstLastDay =()=>{
var firstDay = new Date();
firstDay.setDate(1);
firstDay.setMonth(0);
var lastDay = new Date();
lastDay.setFullYear(lastDay.getFullYear()+1);
lastDay.setDate(0);
lastDay.setMonth(-1);
firstDay = firstDay.Format("yyyy-MM-dd");
lastDay = lastDay.Format("yyyy-MM-dd");
return (firstDay + " " + "00:00:00") + "~" + (lastDay + " " + "23:59:59");
}