JS:獲取時間格式utils工具包。

1:生成普通時間格式數據.

//生成時間格式
function getTime() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var Hours = date.getHours();
    var Minutes = date.getMinutes();
    var Seconds = date.getSeconds();
    //描述一下,在生成時間格式時,如果小時數小於10,例:2020-4-18 9:58:8 是這樣的格式對象。在數據庫排序查詢時會有混亂,
    // A:2020-4-18 9:58:8  B:2020-04-18 08:58:08  查詢出來,A會在B的前面。
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (Hours >= 0 && Hours <= 9) {
        Hours = "0" + Hours;
    }
    if (Minutes >= 0 && Minutes <= 9) {
        Minutes = "0" + Minutes;
    }
    if (Seconds >= 0 && Seconds <= 9) {
        Seconds = "0" + Seconds;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + Hours + seperator2 + Minutes + seperator2 + Seconds;
    return currentdate;
}
console.log(getTime())
//得到2020-04-18 10:00:58

2.獲取減去15天的時間格式數據.

/**
 * 獲取減去多少天的時間。
 * @param {*} fmt 格式化參數
 * @param {*} numbers 減去多少天
 */
function format(fmt, numbers) {
    var date = new Date()
    date.setDate(numbers)
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小時
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}
console.log(format("yyyy-MM-dd hh:mm:ss", new Date().getDate() - 15))
//現在我的時間是2020-04-18 10:04:55 得到 2020-04-03 10:04:55

3.生成本月第一天,最後一天,上月第一天,最後一天時間格式數據。

/**
 * 生成 本月,上月時間
 * @param {*} dateSize 
 */
function TimeFormat(str) {
    var date = new Date();
    if (str === "lastMonthMin") {
        if (date.getMonth() + 1 === 1) {
            var Year = new Date().getFullYear() - 1;
            return Year + "-" + "12" + "-01 00:00:00"
        } else {
            var Year = new Date().getFullYear();
            var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + ((new Date().getMonth() + 1) - 1))
            return Year + "-" + Month + "-01 00:00:00"
        }
    }
    if (str === "lastMonthMax") {
        if (date.getMonth() + 1 === 1) {
            var Year = new Date().getFullYear() - 1;
            //獲取本月天數(獲取後一個月的0日即前一月的最後一日)
            var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
            return Year + "-" + "12" + "-" + totalDay + " 23:59:59"
        } else {
            var Year = new Date().getFullYear();
            var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + ((new Date().getMonth() + 1) - 1))
            //獲取本月天數(獲取後一個月的0日即前一月的最後一日)
            var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
            return Year + "-" + Month + "-" + totalDay + " 23:59:59"
        }
    }
    if (str === "thisMonthMin") {
        var Year = new Date().getFullYear();
        var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + (new Date().getMonth() + 1))
        return Year + "-" + Month + "-01 00:00:00"
    }
    if (str === "thisMonthMax") {
        var Year = new Date().getFullYear();
        var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + (new Date().getMonth() + 1));
        var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
        return Year + "-" + Month + "-" + totalDay + " 23:59:59"
    }
}
console.log(TimeFormat("lastMonthMin"))
console.log(TimeFormat("lastMonthMax"))
console.log(TimeFormat("thisMonthMin"))
console.log(TimeFormat("thisMonthMax"))
//2020-03-01 00:00:00
//2020-03-30 23:59:59
//2020-04-01 00:00:00
//2020-04-30 23:59:59

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章