时间戳转成中国标准时间
第一种:
1、假如在utils/factory.js写了方法
export function formatDate(fmt){
let date = new Date(fmt);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
month < 10 && (month = '0' + month);
day < 10 && (day = '0' + day);
hours < 10 && (hours = '0' + hours);
minutes < 10 && (minutes = '0' + minutes);
seconds < 10 && (seconds = '0' + seconds);
let format = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return format;
}
2、在想使用这个方法的.vue文件里,按需引入
import {formatDate} from "../../utils/factory"
// test() {
let date = formatDate(new Date(1622887200000)); // 携带参数时间戳
console.log(date);
// }
第二种:
export default {
install: function (Vue) {
Vue.prototype.formatDate = function (fmt) {
let date = new Date(fmt);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
month < 10 && (month = '0' + month);
day < 10 && (day = '0' + day);
hours < 10 && (hours = '0' + hours);
minutes < 10 && (minutes = '0' + minutes);
seconds < 10 && (seconds = '0' + seconds);
let format = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return format;
}
}
}
main.js引入
import factory from "@/utils/factory";
Vue.use(factory)
在想使用这个方法的.vue文件
let date = this.formatDate(new Date(res.value.time)); //此为时间戳
console.log(date);
输出pdf格式下载
1、假如在utils/factory.js写了方法
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default {
install: function (Vue) {
Vue.prototype.getPdf = function(id,title){
html2Canvas(document.querySelector(`#${id}`), {
allowTaint: true
}).then(function (canvas) {
let contentWidth = canvas.width
let contentHeight = canvas.height
let pageHeight = contentWidth / 592.28 * 841.89
let leftHeight = contentHeight
let position = 0
let imgWidth = 595.28
let imgHeight = 592.28 / contentWidth * contentHeight
let pageData = canvas.toDataURL('image/jpeg', 1.0)
let PDF = new JsPDF('', 'pt', 'a4')
if (leftHeight < pageHeight) {
PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
} else {
while (leftHeight > 0) {
PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight
position -= 841.89
if (leftHeight > 0) {
PDF.addPage()
}
}
}
PDF.save(title + '.pdf')
}
)
}
}
}
2、main.js中引入
import factory from "./utils/factory"
Vue.use(factory);
3、在想使用这个方法的.vue文件
// test() {
this.getPdf(div的id,title)
// this.getPdf('charts',this.formData.name)
// }
4、别忘记下载
npm install --save html2canvas
npm install jspdf --save
导出文件-调接口
export() {
window.location.herf = this.urls.接口 + '?参数1=' + xx + '&参数2=' + xxxx
或
window.location.herf = `${this.urls.接口}?参数1=xx&参数2=xxxx`
}