let dateTimeCount = {'year': 1, 'month': 2, 'day': 3, 'hour': 4, 'minute': 5, 'second': 6, 'millisecond': 7};
/**
* 格式化数字(补0)
*
* @param x 待格式化的数字
* @param y 结果的位数(默认为2)
* @param o 在数字前补0(默认为true,false为在数字后补0)
* @return String 格式化后的字符串
*/
function formatInt(x, y, o) {
y = y && y > 0 ? y : 2;
o = o === false || o === true ? o : true;
let xArr = String(x).split(''), zCount = y - xArr.length > 0 ? y - xArr.length : 0;
for (let i = 0; i < zCount; i++) {
if (o === true) {
xArr.unshift(0);
} else {
xArr.push(0);
}
}
return xArr.join('');
}
//日期转为字符串
function dateToString(date, option) {
let result = null, count = 6;
if (date) {
if (valueType(date, 'Number') === true) {
date = new Date(date)
}
if (valueType(date, 'Date') === true) {
if (valueType(option, 'String') === true) {
if (option.startsWith('begin')) {
count = parseInt(option.substr('begin'.length)) ? parseInt(option.substr('begin'.length)) : dateTimeCount[option.substr('begin'.length + 1)];
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
} else if (option.startsWith('end')) {
count = parseInt(option.substr('end'.length)) ? parseInt(option.substr('end'.length)) : dateTimeCount[option.substr('end'.length + 1)];
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
date.setMilliseconds(999);
} else {
count = parseInt(option) ? parseInt(option) : dateTimeCount[option];
}
}
if (valueType(option, 'Number') === true) {
count = option;
}
let dateTimeArray = [String(date.getFullYear()), '-' + formatInt(date.getMonth() + 1), '-' + formatInt(date.getDate()),
' ' + formatInt(date.getHours()), ':' + formatInt(date.getMinutes()), ':' + formatInt(date.getSeconds()), '.' + formatInt(date.getMilliseconds(), 3)];
result = '';
// count = count ? (count > 7 ? 6 : count < 1 ? 6 : count) : 6;
count = count && count <= 7 && count >= 1 ? count : 6;
for (let i = 0; i < count; i++) {
result += dateTimeArray[i];
}
}
}
return result;
}
//字符串转为日期
function stringToDate(str) {//str的格式为yyyy-MM-dd HH:mm:ss.msms或yyyy-MM-dd HH:mm:ss或yyyy-MM-dd或HH:mm:ss
let date = new Date();
if (str === 'today_begin') {
return stringToDate(dateToString(date).substr(0, 10) + ' 00:00:00.000');
}
if (str === 'today_end') {
return stringToDate(dateToString(date).substr(0, 10) + ' 23:59:59.999');
}
if (str === 'today') {
return date;
}
if (!str || valueType(str, 'String') === false) {
return null;
}
let year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(),
hour = date.getHours(), minute = date.getMinutes(), second = date.getSeconds(), millisecond = 0;
if (str.length === 10) {
year = str.substr(0, 4);
month = str.substr(5, 7);
day = str.substr(8, 10);
}
if (str.length === 8) {
hour = str.substr(0, 2);
minute = str.substr(3, 5);
second = str.substr(6, 8);
}
if (str.length === 19) {
year = str.substr(0, 4);
month = str.substr(5, 7);
day = str.substr(8, 10);
hour = str.substr(11, 13);
minute = str.substr(14, 16);
second = str.substr(17, 19);
}
if (str.length === 23) {
year = str.substr(0, 4);
month = str.substr(5, 7);
day = str.substr(8, 10);
hour = str.substr(11, 13);
minute = str.substr(14, 16);
second = str.substr(17, 19);
millisecond = str.substr(20, 23)
}
return new Date(parseInt(year), parseInt(month) - 1, parseInt(day), parseInt(hour), parseInt(minute), parseInt(second), parseInt(millisecond));
}
//获取任意一天的结束时间
// time为某一天的时间戳
function startTime(startTime) {
const nowTimeDate = new Date(startTime)
return nowTimeDate.setHours(0, 0, 0, 0)
}
//获取任意一天的结束时间
function endTime(endTime) {
const nowTimeDate = new Date(endTime)
return nowTimeDate.setHours(23, 59, 59, 999)
}
//某年某月最大天数
function lastday(year, month) {
let d = new Date(parseInt(year), parseInt(month), 1, 0, 0, 0, 0);
return new Date(d.getTime() - 1000).getDate();
}