const DateTool = {};
|
|
DateTool.getDate = function() {
|
var date = new Date();
|
var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
|
var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
|
var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
|
var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
|
var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
|
var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
|
|
//获取当前系统时间
|
//var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
|
//alert(currentDate);
|
//修改月份格式
|
if (month >= 1 && month <= 9) {
|
month = "0" + month;
|
}
|
|
//修改日期格式
|
if (day >= 0 && day <= 9) {
|
day = "0" + day;
|
}
|
|
//修改小时格式
|
if (hours >= 0 && hours <= 9) {
|
hours = "0" + hours;
|
}
|
|
//修改分钟格式
|
if (minutes >= 0 && minutes <= 9) {
|
minutes = "0" + minutes;
|
}
|
|
//修改秒格式
|
if (seconds >= 0 && seconds <= 9) {
|
seconds = "0" + seconds;
|
}
|
|
//获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
|
var currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
|
return currentFormatDate;
|
}
|
DateTool.GetDateDiff = function(startTime, endTime, diffType) {
|
//将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
|
startTime = startTime.replace(/\-/g, "/");
|
endTime = endTime.replace(/\-/g, "/");
|
|
//将计算间隔类性字符转换为小写
|
diffType = diffType.toLowerCase();
|
var sTime = new Date(startTime); //开始时间
|
var eTime = new Date(endTime); //结束时间
|
//作为除数的数字
|
var divNum = 1;
|
switch (diffType) {
|
case "second":
|
divNum = 1000;
|
break;
|
case "minute":
|
divNum = 1000 * 60;
|
break;
|
case "hour":
|
divNum = 1000 * 3600;
|
break;
|
case "day":
|
divNum = 1000 * 3600 * 24;
|
break;
|
default:
|
break;
|
}
|
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
|
}
|
|
DateTool.getTodayDate = function() {
|
var date = new Date();
|
var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
|
var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
|
var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
|
//修改月份格式
|
if (month >= 1 && month <= 9) {
|
month = "0" + month;
|
}
|
|
//修改日期格式
|
if (day >= 0 && day <= 9) {
|
day = "0" + day;
|
}
|
//获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
|
var currentFormatDate = year + "-" + month + "-" + day ;
|
return currentFormatDate;
|
}
|
DateTool.AddMinute = function(date, num) {
|
let d = new Date(date.getTime());
|
let newD = new Date(d.setMinutes(d.getMinutes() + num)); //设置天数 +1 天
|
return formatDate(newD, 'yyyy-MM-dd HH:mm:ss');
|
}
|
|
DateTool.formatDate = function(date, fmt) {
|
if (/(y+)/.test(fmt)) {
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
}
|
let o = {
|
'M+': date.getMonth() + 1,
|
'd+': date.getDate(),
|
'h+': date.getHours(),
|
'm+': date.getMinutes(),
|
's+': date.getSeconds()
|
};
|
|
for (let k in o) {
|
if (new RegExp(`(${k})`).test(fmt)) {
|
let str = o[k] + '';
|
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
|
}
|
}
|
return fmt;
|
}
|
DateTool.padLeftZero = function(str) {
|
return ("00" + str).substr(str.length);
|
}
|
export default DateTool;
|