一些公用方法(自用)
程序员文章站
2024-03-17 15:47:34
...
一些公用方法(自用)
持续更新
//公用方法
//Date Wed Mar 17 2021 13:52:00 GMT+0800 (中国标准时间) 转标准的日期格式
export function dateFormat(date){
let y = date.getFullYear()
let m = date.getMonth() + 1
m = m < 10 ? ('0' + m) : m
let d = date.getDate()
d = d < 10 ? ('0' + d) : d
let h =date.getHours()
h = h < 10 ? ('0' + h) : h
let M =date.getMinutes()
M = M < 10 ? ('0' + M) : M
let s =date.getSeconds()
s = s < 10 ? ('0' + s) : s
let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
return dateTime
}
//获取可视区的宽高
export function getViewPortOffset() {
if (window.innerWidth) {
return {
w: window.innerWidth,
h: window.innerHeight
}
} else if (document.compatMode = "BackCompat") {
return {
w: document.body.clientWidth,
h: document.body.clientHeigth
}
} else {
return {
w: document.documentElement.clientWidth,
h: document.documentElement.clientHeight
}
}
}
//完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五。
export function toChinesNum(num) {
let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
let unit = ["", "十", "百", "千", "万"];
num = parseInt(num);
let getWan = (temp) => {
let strArr = temp.toString().split("").reverse();
let newNum = "";
for (var i = 0; i < strArr.length; i++) {
newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
}
return newNum;
}
let overWan = Math.floor(num / 10000);
let noWan = num % 10000;
if (noWan.toString().length < 4) noWan = "0" + noWan;
return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}
// 使用
import { toChinesNum } from '@/components/publicMethod.js'//公用方法(方法名)