提交 3da1427a 编写于 作者: 小刘28's avatar 小刘28 💬

feat:添加日期时间处理库;

上级 c6e03daa
/**
* @Author: xiaozuo28
* @Date: 2022年5月13日08:05:20
* @LastEditors: xiaozuo28
* @LastEditTime: 2022年7月19日23:47:40
* @Description: 日期时间处理库
**/
export default {
/**
* @description 格式化时间 年,月,日,小时,分钟,秒,季度
* @param {String} format 时间格式
* @param {Date} date 待格式化时间
* @return {String} 格式化的时间
**/
formatDateTime ( format, dateTime ) {
let ret;
let opt = {
/* 年份 */
'Y+': dateTime.getFullYear( ).toString( ),
/* 月份 */
'M+': ( dateTime.getMonth( ) + 1 ).toString( ),
/* 天 */
'D+': dateTime.getDate( ).toString( ),
/* 小时 */
'H+': dateTime.getHours( ).toString( ),
/* 分钟 */
'm+': dateTime.getMinutes( ).toString( ),
/* 秒 */
's+': dateTime.getSeconds( ).toString( ),
/* 毫秒 */
'S': dateTime.getMilliseconds( ),
/* 季度 */
'q+': Math.floor( ( dateTime.getMonth( ) + 3 ) / 3 )
};
for ( let k in opt ) {
ret = new RegExp( '(' + k + ')' ).exec( format );
if ( ret ) {
format = format.replace( ret[ 1 ], ( ret[ 1 ].length == 1 ) ? ( opt[ k ] ) : ( opt[ k ].padStart( ret[ 1 ].length, "0" ) ) );
}
}
return format;
},
/**
* @description 格式化秒 最大到天
* @param {Number} value 待格式化秒
* @return {String} 格式化后的时间
**/
formatSeconds(value) {
let theTime = parseInt(value); // 需要转换的时间秒
let theTime1 = 0; // 分
let theTime2 = 0; // 小时
let theTime3 = 0; // 天
if (theTime > 60) {
theTime1 = parseInt(theTime / 60);
theTime = parseInt(theTime % 60);
if (theTime1 > 60) {
theTime2 = parseInt(theTime1 / 60);
theTime1 = parseInt(theTime1 % 60);
if (theTime2 > 23) {
//大于24小时
theTime3 = parseInt(theTime2 / 24);
theTime2 = parseInt(theTime2 % 24);
}
}
}
let result = '';
if (theTime > 0) {
result = "" + parseInt(theTime) + "";
}
if (theTime1 > 0) {
result = "" + parseInt(theTime1) + "" + result;
}
if (theTime2 > 0) {
result = "" + parseInt(theTime2) + "小时" + result;
}
if (theTime3 > 0) {
result = "" + parseInt(theTime3) + "" + result;
}
return result;
},
/**
* @description 获取月份的天数
* @param {String} time 月份时间,示例:2020-01
* @return {Number} 天数
*/
getMonthOfDay(yearMonth) {
let date = new Date(yearMonth);
let year = date.getFullYear();
let mouth = date.getMonth() + 1;
let days;
//当月份为二月时,根据闰年还是非闰年判断天数
if (mouth == 2) {
days = (year % 4 == 0 && year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ? 28 : 29;
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
//月份为:1,3,5,7,8,10,12 时,为大月.则天数为31;
days = 31;
} else {
//其他月份,天数为:30.
days = 30;
}
return days;
},
/**
* @description 获取一年有多少天
* @param {String} time 年份;示例:2020
* @return {Number} 天数
*/
getYearOfDay(year) {
let firstDayYear = this.getFirstDayOfYear(year);
let lastDayYear = this.getLastDayOfYear(year);
let numSecond = (new Date(lastDayYear).getTime() - new Date(firstDayYear).getTime()) / 1000;
return Math.ceil(numSecond / (24 * 3600));
},
/**
* @description 获取某年的第一天
* @param {String} year 年份;示例:2020
* @return {String} 某年的第一天
*/
getFirstDayOfYear(year) {
let year = new Date(year).getFullYear();
return year + "/01/01 00:00:00";
},
/**
* @description 获取某年的最后一天
* @param {String} time 年份;示例:2020
* @return {String} 某年的最后一天
*/
getLastDayOfYear(time) {
let year = new Date(time).getFullYear();
let dateString = year + "/12/01 00:00:00";
let endDay = this.getMonthOfDay(dateString);
return year + "/12/" + endDay + " 23:59:59";
},
/**
* @description 获取某个日期是当年中的第几天
* @param {String} time 日期
* @return {Number} 第几天
*/
getDayOfYear(time) {
let firstDayYear = this.getFirstDayOfYear(time);
let numSecond = (new Date(time).getTime() - new Date(firstDayYear).getTime()) / 1000;
return Math.ceil(numSecond / (24 * 3600));
},
/**
* @description 获取某个日期在这一年的第几周
* @param {String} time 日期
* @return {Number} 第几周
*/
getDayOfYearWeek(time) {
let numdays = this.getDayOfYear(time);
return Math.ceil(numdays / 7);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册