export const formatDate = (date, fmt) => {
    date = new Date(date);
    if (typeof fmt === 'undefined') {
        fmt = 'yyyy-MM-dd HH:mm:ss';
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        Y: date.getFullYear(),
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length));
        }
    }
    return fmt;
};

//时间转换成日-时
export const formatDateTime = (inputTime) => {
    if (inputTime != null) {
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        var h = date.getHours();
        h = h < 10 ? '0' + h : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;
        return d + '日' + h + '时';
    }
};
//时间转换成月-日-时
export const formatMonthTime = (inputTime) => {
    if (inputTime != null) {
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        var h = date.getHours();
        h = h < 10 ? '0' + h : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;
        return m + '月' + d + '日' + h + '时';
    }
};
//时间戳 时间转换
export const timeDateTo = (inputTime) => {
    function p(s) {
        return s < 10 ? '0' + s : s;
    }
    if (inputTime != null) {
        var date = new Date(inputTime);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hours = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return year + '-' + p(month) + '-' + p(day) + ' ' + p(hours) + ':' + p(minute);
    }
};

/**
 * 获取起始时间点
 * @param {string} selectedTime  项目中选择的时间
 * @param {number} hours 时间差，时间范围
 * @returns 返回起始时间点
 */
export const getStartTime = (selectedTime, hours) => {
    selectedTime = selectedTime.replace(new RegExp('-', 'gm'), '/');
    var startDate = new Date(selectedTime).getTime();
    var d3 = startDate - hours * 3600 * 1000;
    var endDate = timestampToTime(d3);
    return endDate;
};
/**
 * 获取终止时间点
 * @param {string} selectedTime  项目中选择的时间
 * @param {number} hours 时间差，时间范围
 * @returns 返回起始时间点
 */
export const getEndTime = (selectedTime, hours) => {
    selectedTime = selectedTime.replace(new RegExp('-', 'gm'), '/');
    var startDate = new Date(selectedTime).getTime();
    var d3 = startDate + hours * 3600 * 1000;
    var endDate = timestampToTime(d3);
    return endDate;
};
//将时间戳转换成正常时间格式;
function timestampToTime(timestamp) {
    var date = new Date(timestamp);
    var Y = date.getFullYear() + '-';
    var M = timeAdd0((date.getMonth() + 1).toString()) + '-';
    var D = timeAdd0(date.getDate().toString()) + ' ';
    var h = timeAdd0(date.getHours().toString()) + ':';
    var m = timeAdd0(date.getMinutes().toString()) + ':';
    var s = timeAdd0(date.getSeconds().toString());
    return Y + M + D + h + m + s;
}
//补齐十位数;
function timeAdd0(str) {
    if (str.length <= 1) {
        str = '0' + str;
    }
    return str;
}

export const getBetweenHours = (startDateStr, endDateStr) => {
    let hours = 0;
    let date11 = new Date(startDateStr).getTime();
    let date22 = new Date(endDateStr).getTime();
    hours = parseInt(Math.abs(date22 - date11) / (1000 * 60 * 60));
    return hours;
};
