JS date 官方地址
/**
* 获取当天所在月的最后一天日期,和下一个月的第一天日期
* 调用getMonthDay(d), d默认值为01,d为要设置成的day;
*/
const getMonthMaxDay = () => {
var curDate = new Date();
var curMonth = curDate.getMonth();
curDate.setMonth(curMonth + 1);
curDate.setDate(0);
return curDate.getDate();
}
export const getMonthDay = (d='01') => {
const t = new Date();
return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${d}`;
}
const setT = (e) => {
return `${e}`.length > 1 ? `${e}` : `0${e}`;
}
getMonthDay()
getMonthDay(getMonthMaxDay);
/**
* 获取当天所在周的周一和周日的日期
* 返回 YYYY-MM-DD 格式时间
*/
const setToday = function (t=(new Date())) {
return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${setT(t.getDate())}`;
}
export const getWeekDay = () => {
const date1 = new Date(),
date2 = new Date();
return {
weekStart: setToday(new Date(date1.setDate(date1.getDate() - date1.getDay() + 1))),
weekEnd: setToday( new Date(date2.setDate(date2.getDate() + (7 - date2.getDay())))),
};
}
getWeekDay()
/**
* 对时间进行加减运算
* calcTime(t, d) t参数为要进行计算的日期,d为时间差
* 返回 YYYY-MM-DD HH:mm:ss 格式时间
*/
const setFullTime = function(t=(new Date())) {
return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${setT(t.getDate())} ${setT(t.getHours())}:${setT(t.getMinutes())}:${setT(t.getSeconds())}`;
}
export const calcTime = (t, d) => {
var date1 = new Date((t).replace(/-/g, '/')),
date2 = (date1/1000 + d)*1000,
date3 = new Date(date2);
return setFullTime(date3);
};
const d1 = '2017-05-26 18:08:45';
cosnt d = -30;
calcTime(d1, d)