获取日期年月日时分秒
const year = date.getFullYear();
const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
const millisecond = date.getMilliseconds();
const weekday = date.getDay();
增加或者是减少日期小时数
export function dateAddHour(date, hour) {
if (date == null) return null
var curTime = new Date(date)
var addHour = curTime.setHours(curTime.getHours() + hour);
return new Date(addHour)
}
增加或者是减少天数
export function dateAddDay(date, day) {
if (date == null) return null
var curTime = new Date(date);
curTime.setDate(curTime.getDate() + day);
return new Date(curTime)
}
格式化日期格式(yyyy-MM-dd HH:mm:ss)
export function formatDate1(date) {
if (date == null) return null
const year = date.getFullYear();
const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
const millisecond = date.getMilliseconds();
const weekday = date.getDay();
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
格式化日期格式(yyyy-MM-dd)
export function formatDate2(date) {
if (date == null) return null
const year = date.getFullYear();
const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
const millisecond = date.getMilliseconds();
const weekday = date.getDay();
return year + '-' + month + '-' + day;
}
生成时间戳
export function getTimeStamp(date){
return date.valueOf()
}
获取当月第一天
export function getDateFirst(date) {
date.setDate(1);
return formatDate2(date)
}