/**
|
* @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
|
*/
|
function dateTimeStr(str){
|
var date = new Date(),
|
year = date.getFullYear(), //年
|
month = date.getMonth() + 1, //月
|
day = date.getDate(), //日
|
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
|
minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
|
second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
|
month >= 1 && month <= 9 ? (month = "0" + month) : "";
|
day >= 0 && day <= 9 ? (day = "0" + day) : "";
|
hour >= 0 && hour <= 9 ? hour : "";
|
minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
|
second >= 0 && second <= 9 ? (second = "0" + second) : "";
|
if(str.indexOf('y') != -1){
|
str = str.replace('y', year)
|
}
|
if(str.indexOf('m') != -1){
|
str = str.replace('m', month)
|
}
|
if(str.indexOf('d') != -1){
|
str = str.replace('d', day)
|
}
|
if(str.indexOf('h') != -1){
|
str = str.replace('h', hour)
|
}
|
if(str.indexOf('i') != -1){
|
str = str.replace('i', minute)
|
}
|
if(str.indexOf('s') != -1){
|
str = str.replace('s', second)
|
}
|
return str;
|
}
|
/**
|
* @param {Object} hour 小时的数值
|
* @param {Object} min 分钟的数值。
|
* @param {Object} sec 秒的数值。
|
* @param {Object} millisec 毫秒的数值。
|
* @param {Object} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
|
*/
|
function dateTimeSetHoureStr(hour,min,sec,millisec,str){
|
var date = new Date();
|
date.setHours(hour,min,sec,millisec);
|
var year = date.getFullYear(), //年
|
month = date.getMonth() + 1, //月
|
day = date.getDate(), //日
|
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
|
minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
|
second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
|
month >= 1 && month <= 9 ? (month = "0" + month) : "";
|
day >= 0 && day <= 9 ? (day = "0" + day) : "";
|
hour >= 0 && hour <= 9 ? hour : "";
|
minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
|
second >= 0 && second <= 9 ? (second = "0" + second) : "";
|
if(str.indexOf('y') != -1){
|
str = str.replace('y', year)
|
}
|
if(str.indexOf('m') != -1){
|
str = str.replace('m', month)
|
}
|
if(str.indexOf('d') != -1){
|
str = str.replace('d', day)
|
}
|
if(str.indexOf('h') != -1){
|
str = str.replace('h', hour)
|
}
|
if(str.indexOf('i') != -1){
|
str = str.replace('i', minute)
|
}
|
if(str.indexOf('s') != -1){
|
str = str.replace('s', second)
|
}
|
return str;
|
}
|
/** 计算两者时间差以小时为单位
|
* @param {Object} beginStr YYYY-MM-DD HH:MM
|
* @param {Object} endStr YYYY-MM-DD HH:MM
|
*/
|
function calculateHoursDiff(beginStr, endStr) {
|
// 解析开始时间
|
const parseDateTime = (dateTimeStr) => {
|
const [datePart, timePart] = dateTimeStr.split(' ');
|
const [yyyy, mm, dd] = datePart.split('-').map(Number);
|
const [hh, min] = timePart.split(':').map(Number);
|
return new Date(yyyy, mm - 1, dd, hh, min);
|
};
|
|
const beginTime = parseDateTime(beginStr);
|
const endTime = parseDateTime(endStr);
|
|
// 计算毫秒差,并转换为小时(保留2位小数)
|
const diffMs = endTime - beginTime;
|
const diffHours = (diffMs / (1000 * 60 * 60)).toFixed(2);
|
|
return parseFloat(diffHours)>0?parseFloat(diffHours):0; // 返回数字类型,如 3.25
|
}
|
|
/**返回时间加上小时
|
* @param {Object} dateTimeStr YYYY-MM-DD HH:MM
|
* @param {Object} hoursToAdd 小时
|
*/
|
function addHoursToDate(dateTimeStr, hoursToAdd) {
|
// 解析输入日期时间
|
const [datePart, timePart] = dateTimeStr.split(' ');
|
const [yyyy, mm, dd] = datePart.split('-').map(Number);
|
const [hh, min] = timePart.split(':').map(Number);
|
|
// 创建Date对象
|
const date = new Date(yyyy, mm - 1, dd, hh, min);
|
|
// 添加指定小时数(支持小数小时)
|
date.setTime(date.getTime() + hoursToAdd * 60 * 60 * 1000);
|
|
// 格式化为YYYY-MM-DD HH:MM
|
const newYYYY = String(date.getFullYear()).padStart(4, '0');
|
const newMM = String(date.getMonth() + 1).padStart(2, '0');
|
const newDD = String(date.getDate()).padStart(2, '0');
|
const newHH = String(date.getHours()).padStart(2, '0');
|
const newMin = String(date.getMinutes()).padStart(2, '0');
|
|
return `${newYYYY}-${newMM}-${newDD} ${newHH}:${newMin}`;
|
}
|
/** 将时间转换成 YYYY-MM-DD HH:MM格式
|
* @param {Object} date 时间
|
*/
|
function formatDate(date) {
|
const year = date.getFullYear();
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
const day = String(date.getDate()).padStart(2, '0');
|
const hours = String(date.getHours()).padStart(2, '0');
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
}
|
module.exports = {
|
dateTimeStr: dateTimeStr,
|
dateTimeSetHoureStr:dateTimeSetHoureStr,
|
calculateHoursDiff:calculateHoursDiff,
|
addHoursToDate:addHoursToDate,
|
formatDate:formatDate,
|
}
|