
正文
JavaScript 数字转汉字+element时间选择器快速选择
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
window.CN = {
: '一',
: '二',
: '三',
: '四',
: '五',
: '六',
: '七',
: '八',
: '九',
: '零'
}
window.LEVEL = {
: '',
: '十',
: '百',
: '千',
: '万',
: '十',
: '百',
: '千',
: '亿',
: '十',
: '百',
: '千',
}
function toCN(inStr) {
console.dir(inStr)
for(let i in CN)
inStr = (inStr+'').replace(new RegExp(i, 'g'), CN[i]) let result = '', maxIdx = inStr.length-
for(let i=; i<=maxIdx; i++){
let mchar = inStr.charAt(i)
let mlevel = LEVEL[maxIdx-i]
if(inStr.charAt(i)==='零'){
if((maxIdx-i)% === )
result += i===maxIdx ? '' : mlevel
else
result += mchar
}else{
result += mchar+mlevel
}
}
return result.replace('一十','十')
}
console.dir(toCN())
console.dir(toCN())
console.dir(toCN())
结果:

elementUI 时间选择器快速选择选择器代码
<el-date-picker v-model="timeRange" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" format="yyyy-MM-dd HH" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
相关参数
timeRange: [],
keyStr: '',
times: [
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '天'},
{value: , unit: '天'},
{value: , unit: '天'},
{value: , unit: '月'},
{value: , unit: '月'},
],
pickerOptions: {
shortcuts: []
}
相关方法
createTimes(){
this.pickerOptions.shortcuts = []
this.times.forEach(t=>{
this.pickerOptions.shortcuts.push({
text: '最近'+TOCN(t.value)+t.unit,
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - t.value*TIMETYPE[t.unit]);
picker.$emit('pick', [start, end]);
}
})
})
}created中调用
效果图
<el-date-picker v-model="timeRange" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" format="yyyy-MM-dd HH" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
timeRange: [],
keyStr: '',
times: [
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '小时'},
{value: , unit: '天'},
{value: , unit: '天'},
{value: , unit: '天'},
{value: , unit: '月'},
{value: , unit: '月'},
],
pickerOptions: {
shortcuts: []
}
createTimes(){
this.pickerOptions.shortcuts = []
this.times.forEach(t=>{
this.pickerOptions.shortcuts.push({
text: '最近'+TOCN(t.value)+t.unit,
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - t.value*TIMETYPE[t.unit]);
picker.$emit('pick', [start, end]);
}
})
})
}created中调用
选择最近一个月

选择最近三小时







