/**
* 时间工具
* @author bamboo
*/
export default {
/** 补零 */
addZero : function(num) {
return num < 10 ? '0' + num : num;
},
/** 获取两位数的月份 */
getMonthByMM : function(date) {
return this.addZero(date.getMonth() + 1);
},
/** 获取两位数的日 */
getDayByDD : function(date){
return this.addZero(date.getDate());
},
/** 获取两位数的小时 */
getHourseByHH : function(date) {
return this.addZero(date.getHours())
},
/** 获取两位数的分钟 */
getMinutesByMI : function(date) {
return this.addZero(date.getMinutes());
},
/** 获取两位数的分钟 */
getSecondsBySS : function(date) {
return this.addZero(date.getSeconds());
},
/** 获取当前时间 */
getYYYYMM : function(date) {
return date.getFullYear()
+ "-" + this.getMonthByMM(date);
},
/** 获取当前时间 */
getYYYYMMDD : function(date) {
return this.getYYYYMM(date) + "-" + this.getDayByDD(date);
},
/** 获取当前时间 */
getHHMISS : function(date) {
return this.getHourseByHH(date)
+ ":" + this.getMinutesByMI(date)
+ ":" + this.getSecondsBySS(date);
},
/** 获取当前时间 */
getYYYYMMDDHHMISS : function(date) {
return this.getYYYYMMDD(date) + " " + this.getHHMISS(date);
},
/** 根据时间格式获取时间 支持常用格式 */
getDateDateByFormat : function(format) {
let date = new Date();
let dateStr = '';
switch(format) {
case 'yyyy-MM-dd HH:mm:ss' :
dateStr = this.getYYYYMMDDHHMISS(date);
break;
case 'YYYY-MM-DD' :
dateStr = getYYYYMMDD(date);
break;
case 'YYYY-MM' :
dateStr = getYYYYMM(date);
break;
case 'YYYY' :
dateStr = date.getFullYear();
break;
case 'YYYY/MM/DD' :
dateStr = date.getFullYear()
+ "/" + this.getMonthByMM(date)
+ "/" + this.getDayByDD(date);
break;
default:;
}
return dateStr;
}
}
JS时间工具
最新推荐文章于 2024-07-04 11:21:17 发布