chenhaozhe
2025-08-07 96a8728645134ac212762daffc16ceceea710e0a
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
/**
 * @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;
}
 
module.exports = {
    dateTimeStr: dateTimeStr,
}