- 年月日时间格式化
- 时分秒格式化
- 时间戳转为多久之前
- 获取静态git图片
- 阿拉伯数字转换为中文
- rem引入
- 通过身份证获取年龄
- 通过身份证获取生日
- 通过身份证获取性别
10.获取指定时间
年月日时间格式化
export function formatDate(date, type){
if (!date) {
return false
}
date = new Date(date)
let year = date.getFullYear()
let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
if (type === 1) {
return year + '-' + month + '-' + day
} else if (type === 2) {
return year + '年' + month + '月' + day + '日' + hour + '时' + minutes + '分'
} else if (type === 3) {
return year + '' + month + '' + day + '' + hour + '' + minutes + '' + seconds
} else if (type === 4) {
return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes
} else if (type === 5) {
return year + '-' + '01-01'
} else {
return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
}
}
时分秒格式化
export const realFormatSecond = (second) => {
var secondType = typeof second
if (secondType === "number" || secondType === "string") {
second = parseInt(second)
var hours = Math.floor(second / 3600)
second = second - hours * 3600
var mimute = Math.floor(second / 60)
second = second - mimute * 60
return ("0" + mimute).slice(-2) + ":" + ("0" + second).slice(-2)
} else {
return "0:00:00"
}
}
时间戳转为多久之前
export function timeFrom (timestamp = null, format = 'yyyy-mm-dd') {
if (timestamp == null) timestamp = Number(new Date())
timestamp = parseInt(timestamp)
if (timestamp.toString().length === 10) timestamp *= 1000
var timer = (new Date()).getTime() - timestamp
timer = parseInt(timer / 1000)
let tips = ''
switch (true) {
case timer < 300:
tips = '刚刚'
break
case timer >= 300 && timer < 3600:
tips = parseInt(timer / 60) + '分钟前'
break
case timer >= 3600 && timer < 86400:
tips = parseInt(timer / 3600) + '小时前'
break
case timer >= 86400 && timer < 2592000:
tips = parseInt(timer / 86400) + '天前'
break
default:
if (format === false) {
if (timer >= 2592000 && timer < 365 * 86400) {
tips = parseInt(timer / (86400 * 30)) + '个月前'
} else {
tips = parseInt(timer / (86400 * 365)) + '年前'
}
} else {
tips = moment(timestamp, format)
}
}
return tips
}
获取静态git图片
export const getGifData = function (url) {
return new Promise((resolve, reject) => {
let ajax = null
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest()
} else if (window.ActiveXObject) {
ajax = new window.ActiveXObject()
} else {
alert('您的浏览器不支持ajax')
}
if (ajax != null) {
ajax.open('GET', url, true)
ajax.responseType = "blob"
ajax.onload = function () {
if (this.readyState !== 4) { return }
var reader = new FileReader()
reader.onload = function () {
resolve(this.result)
}
reader.readAsDataURL(this.response)
}
ajax.send()
} else {
reject(new Error('error'))
}
})
}
阿拉伯数字转换为中文
export const sectionToChinese = (section) => {
var chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
var chnUnitChar = ['', '十', '百', '千']
var strIns = ''
var chnStr = ''
var unitPos = 0
var zero = true
while (section > 0) {
var v = section % 10
if (v === 0) {
if (!zero) {
zero = true
chnStr = chnNumChar[v] + chnStr
}
} else {
zero = false
strIns = chnNumChar[v]
strIns += chnUnitChar[unitPos]
chnStr = strIns + chnStr
}
unitPos++
section = Math.floor(section / 10)
}
if (chnStr.startsWith('一十')) {
chnStr = chnStr.replace('一十', '十')
}
return chnStr
}
rem引入
import '@/js/rem.js'
(function() {
const orientation = window.matchMedia('(orientation: portrait)')
let width = document.documentElement.getBoundingClientRect().width
function onMatchMeidaChange(orientation) {
if (orientation.matches) {
width = document.documentElement.getBoundingClientRect().width
setTimeout(() => {
autoRootFontSize(width)
})
} else {
width = document.documentElement.getBoundingClientRect().width
setTimeout(() => {
autoRootFontSize(width)
})
}
}
onMatchMeidaChange(orientation)
orientation.addListener(onMatchMeidaChange)
function autoRootFontSize(width) {
const baseSize = 16
const scale = document.documentElement.clientWidth / 750
document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
window.addEventListener('resize', autoRootFontSize)
autoRootFontSize()
})()
通过身份证获取年龄
getAge: function(idCard) {
var year1;
idCard = trim(idCard.replace(/ /g, ''));
if (idCard.length == 15) {
year1 = Number('19' + idCard.substring(6, 8));
} else if (idCard.length == 18) {
year1 = Number(idCard.substring(6, 10));
} else {
return null;
}
var year2 = (new Date()).getFullYear();
return (year2 - year1);
},
通过身份证获取生日
getBirth: function(idCard) {
idCard = trim(idCard.replace(/ /g, ''));
var year = '';
var month = '';
var day = '';
if (idCard.length == 15) {
year = idCard.substring(6, 8);
month = idCard.substring(8, 10);
day = idCard.substring(10, 12);
return '19' + year + month + day;
} else if (idCard.length == 18) {
year = idCard.substring(6, 10);
month = idCard.substring(10, 12);
day = idCard.substring(12, 14);
return year + month + day;
} else {
return null;
}
},
通过身份证获取性别
getSex: function(idCard) {
idCard = trim(idCard.replace(/ /g, ''));
if (idCard.length == 15) {
if (idCard.substring(14, 15) % 2 == 0) {
return 2;
} else {
return 1;
}
} else if (idCard.length == 18) {
if (idCard.substring(14, 17) % 2 == 0) {
return 2;
} else {
return 1;
}
} else {
return null;
}
},
获取指定时间
getAppointDate (type, num) {
const date = new Date()
let base = new Date(date).getTime()
const oneDay = 24 * 3600 * 1000
let dateArr = []
const time = new Date(base)
let month = time.getMonth() + 1
if (month < 10) month = '0' + month
dateArr.push([time.getFullYear(), month, time.getDate()].join('-'))
for (let i = 1; i < num; i++) {
let now
if (type === 'before') {
now = new Date(base -= oneDay)
} else if (type === 'after') {
now = new Date(base += oneDay)
}
let _month = now.getMonth() + 1
if (_month < 10) _month = '0' + _month
dateArr.push([now.getFullYear(), _month, now.getDate()].join('-'))
}
return dateArr
}
}