/* 个位补零*/
function Appendzero(obj) {
    if (obj < 10) return "0" + obj;
    else return obj;
}

function add0(m) { return m < 10 ? '0' + m : m }


//时间戳转化成时间格式
function getFormatDate(timestamp) {
    //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(seconds);
}






/* 毫秒转化*/
function test(time_distance) {
    // 天时分秒换算 
    var int_day = Math.floor(time_distance / 86400)
    time_distance = time_distance - int_day * 86400;

    var int_hour = Math.floor(time_distance / 3600)
    time_distance = time_distance - int_hour * 3600;

    var int_minute = Math.floor(time_distance / 60)
    time_distance = time_distance - int_minute * 60;

    var int_second = Math.floor(time_distance)
        // 时分秒为单数时、前面加零 
    if (int_day < 10) {
        int_day = "0" + int_day;
    }
    if (int_hour < 10) {
        int_hour = "0" + int_hour;
    }
    if (int_minute < 10) {
        int_minute = "0" + int_minute;
    }
    if (int_second < 10) {
        int_second = "0" + int_second;
    }
    return int_day + '天' + int_hour + '小时' + int_minute + '分钟' + int_second + '秒';
}


var test = test(3000000);





//限制input输入框字符长度
$("input").attr("maxLength", 20);
//限制textarea输入框字符长度
$("textarea").attr("maxLength", 100);






// 导出封装 
function downExcel(str, titleName) {
    // 输出base64编码
    const base64 = s => window.btoa(unescape(encodeURIComponent(s)));

    // Worksheet名
    const worksheet = 'Sheet1'
    const uri = 'data:application/vnd.ms-excel;base64,';

    // 下载的表格模板数据
    const template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" 
     xmlns:x="urn:schemas-microsoft-com:office:excel" 
     xmlns="http://www.w3.org/TR/REC-html40">
     <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
     <x:Name>${worksheet}</x:Name>
     <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
     </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
     </head><body><table>${str}</table></body></html>`;

    // 通过创建a标签实现
    const link = document.createElement("a");
    link.href = uri + base64(template);
    // 对下载的文件命名
    link.download = titleName + ".xls";
    link.click();

}