util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* 个位补零*/
  2. function Appendzero(obj) {
  3. if (obj < 10) return "0" + obj;
  4. else return obj;
  5. }
  6. function add0(m) { return m < 10 ? '0' + m : m }
  7. //时间戳转化成时间格式
  8. function getFormatDate(timestamp) {
  9. //timestamp是整数,否则要parseInt转换,不会出现少个0的情况
  10. var time = new Date(timestamp);
  11. var year = time.getFullYear();
  12. var month = time.getMonth() + 1;
  13. var date = time.getDate();
  14. var hours = time.getHours();
  15. var minutes = time.getMinutes();
  16. var seconds = time.getSeconds();
  17. return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(seconds);
  18. }
  19. /* 毫秒转化*/
  20. function test(time_distance) {
  21. // 天时分秒换算
  22. var int_day = Math.floor(time_distance / 86400)
  23. time_distance = time_distance - int_day * 86400;
  24. var int_hour = Math.floor(time_distance / 3600)
  25. time_distance = time_distance - int_hour * 3600;
  26. var int_minute = Math.floor(time_distance / 60)
  27. time_distance = time_distance - int_minute * 60;
  28. var int_second = Math.floor(time_distance)
  29. // 时分秒为单数时、前面加零
  30. if (int_day < 10) {
  31. int_day = "0" + int_day;
  32. }
  33. if (int_hour < 10) {
  34. int_hour = "0" + int_hour;
  35. }
  36. if (int_minute < 10) {
  37. int_minute = "0" + int_minute;
  38. }
  39. if (int_second < 10) {
  40. int_second = "0" + int_second;
  41. }
  42. return int_day + '天' + int_hour + '小时' + int_minute + '分钟' + int_second + '秒';
  43. }
  44. var test = test(3000000);
  45. document.addEventListener('input', function(e) {
  46. // input框 type='text'
  47. //e.target.getAttribute('maxlength') === null,本身没有设置maxlength长度,防止全局设置覆盖所在页面设置的长度
  48. if (e.target.type === 'text' && e.target.getAttribute('maxlength') === null) {
  49. e.target.setAttribute('maxlength', '5') // 限制最长输入50个字符
  50. }
  51. // input框 type='textarea',且本身没有设置maxlength长度
  52. if (e.target.type === 'textarea' && e.target.getAttribute('maxlength') === null) {
  53. e.target.setAttribute('maxlength', '100') // 限制最长输入500个字符
  54. }
  55. })
  56. //限制输入框字符长度
  57. $("input").attr("maxLength", 100);
  58. // 导出封装
  59. function downExcel(str, titleName) {
  60. // 输出base64编码
  61. const base64 = s => window.btoa(unescape(encodeURIComponent(s)));
  62. // Worksheet名
  63. const worksheet = 'Sheet1'
  64. const uri = 'data:application/vnd.ms-excel;base64,';
  65. // 下载的表格模板数据
  66. const template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
  67. xmlns:x="urn:schemas-microsoft-com:office:excel"
  68. xmlns="http://www.w3.org/TR/REC-html40">
  69. <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
  70. <x:Name>${worksheet}</x:Name>
  71. <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
  72. </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
  73. </head><body><table>${str}</table></body></html>`;
  74. // 通过创建a标签实现
  75. const link = document.createElement("a");
  76. link.href = uri + base64(template);
  77. // 对下载的文件命名
  78. link.download = titleName + ".xls";
  79. link.click();
  80. }