chenhaozhe
2025-09-12 fb2f2b71c1908cd1630418779a7613cd7970cb96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * @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,
}