123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /**
- * 时间戳转换成时间
- * @param {*} time
- */
- const formDate = (time, formate = "yyyy-mm-dd hh:mm:ss") => {
- var padDate = function (va) {
- va = va < 10 ? "0" + va : va;
- return va;
- };
- if (time) {
- var value = new Date(time * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
- // var value = new Date(parseFloat(time))
- var year = value.getFullYear();
- var month = padDate(value.getMonth() + 1);
- var day = padDate(value.getDate());
- var hour = padDate(value.getHours());
- var minutes = padDate(value.getMinutes());
- var seconds = padDate(value.getSeconds());
- let res = "";
- switch (formate) {
- case "mm-dd": {
- res = month + "-" + day;
- break;
- }
- case "yyyy-mm-dd": {
- res = year + "-" + month + "-" + day;
- break;
- }
- case "yyyy-mm": {
- res = year + "-" + month;
- break;
- }
- case "mm月dd日": {
- res = month + "月" + day + "日";
- break;
- }
- case "yyyy年mm月dd日": {
- res = year + "年" + month + "月" + day + "日";
- break;
- }
- case "yyyy年mm月": {
- res = year + "年" + month + "月";
- break;
- }
- case "hh:mm": {
- res = hour + ":" + minutes;
- break;
- }
- case "yyyy-mm-dd hh:mm": {
- res = year + "-" + month + "-" + day + " " + hour + ":" + minutes;
- break;
- }
- case "yyyy.mm.dd":
- res = year + "." + month + "." + day;
- break;
- case "yyyy-mm-dd hh:mm:ss":
- default: {
- res =
- year +
- "-" +
- month +
- "-" +
- day +
- " " +
- hour +
- ":" +
- minutes +
- ":" +
- seconds;
- break;
- }
- }
- return res;
- }
- return "--";
- };
- const formDateStr = (time) => {
- var padDate = function (va) {
- va = va < 10 ? "0" + va : va;
- return va;
- };
- if (time) {
- var value = new Date(parseFloat(time));
- var year = value.getFullYear();
- var month = padDate(value.getMonth() + 1);
- var day = value.getDate();
- var hour = padDate(value.getHours());
- var minutes = padDate(value.getMinutes());
- var seconds = padDate(value.getSeconds());
- let res = "";
- if (day == new Date().getDate()) {
- res = "今天" + hour + ":" + minutes;
- return res;
- }
- if (year == new Date().getFullYear()) {
- res = month + "-" + padDate(day) + " " + hour + ":" + minutes;
- return res;
- }
- res = year + "-" + month + "-" + padDate(day);
- return res;
- }
- return "--";
- };
- const formatRichText = (html) => {
- //控制小程序中图片大小
- let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
- match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
- match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
- match = match
- .replace(/height="[^"]+"/gi, "")
- .replace(/height='[^']+'/gi, "");
- return match;
- });
- newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
- match = match
- .replace(/width:[^;]+;/gi, "max-width:100%;")
- .replace(/width:[^;]+;/gi, "max-width:100%;");
- return match;
- });
- newContent = newContent.replace(/<br[^>]*\/>/gi, "");
- newContent = newContent.replace(
- /\<img/gi,
- '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"'
- );
- return newContent;
- };
- export default {
- formDate,
- formDateStr,
- formatRichText
- };
|