常用js转换
获取当前日期字符串
function formatDateTime() { var date = new Date(); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return{ 'ymd':y + '-' + m + '-' + d, 'ymdhm':y + '-' + m + '-' + d + ' ' + h + ':' + minute, } }
/^1[3456789]\d{9}$/
export function checkMobile(mobile) {
mobile = mobile.toString();
var index1 = mobile.substring(0, 1);
var index2 = mobile.substring(0, 2);
if (index2 == '11' || index2 == '12' || index1 !== '1' || mobile.length !== 11) {
return false;
} else {
return true;
}
}
export function transData(list, idstr, pidstr) {
var result = []; var temp = {}
// 创建一个对象 以每个元素的idstr建立索引
for (var i = 0; i < list.length; i++) {
temp[list[i][idstr]] = list[i]// 将nodes数组转成对象类型
}
for (var j = 0; j < list.length; j++) {
var tempVp = temp[list[j][pidstr]] // 获取每一个子对象的父对象
if (tempVp) { // 判断父对象是否存在,如果不存在直接将对象放到第一层
if (!tempVp['children']) tempVp['children'] = []// 如果父元素的nodes对象不存在,则创建数组
tempVp['children'].push(list[j])// 将本对象压入父对象的nodes数组
} else {
result.push(list[j])// 将不存在父对象的对象直接放入一级目录
}
}
return result
}
// base64转成二进制
export function dataURItoBlob(dataurl) {
var arr = dataurl.split(',')
// 注意base64的最后面中括号和引号是不转译的
var _arr = arr[1].substring(0, arr[1].length - 2)
var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(_arr)
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], {
type: mime
})
}
// 图片Url转成Base64
export function convertImgToBase64(url, callback, outputFormat) {
var canvas = document.createElement('CANVAS')
var ctx = canvas.getContext('2d')
var img = new Image()
img.crossOrigin = 'Anonymous'
img.onload = function() {
canvas.height = img.height
canvas.width = img.width
ctx.drawImage(img, 0, 0)
var dataURL = canvas.toDataURL(outputFormat || 'image/png')
callback.call(this, dataURL)
canvas = null
}
img.src = url
}
// base64 转 file 对象
export function dataURLtoFile(dataURL, fileName, fileType) {
var arr = dataURL.split(','); var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1]); var n = bstr.length; var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], fileName, { type: fileType || 'image/jpg' })
}
storage.js
var storage = {
set(key, value) {
localStorage.setItem(key, JSON.stringify(value))
},
get(key) {
return localStorage.getItem(key)
},
remove(key) {
localStorage.removeItem(key)
},
clear() {
localStorage.clear()
}
}
export default storage
export function parseTime(time, cFormat) {
if (arguments.length === 0 || !time) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string')) {
if ((/^[0-9]+$/.test(time))) {
// support "1548221490638"
time = parseInt(time)
} else {
// support safari
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
time = time.replace(new RegExp(/-/gm), '/')
}
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
return value.toString().padStart(2, '0')
})
return time_str
}
export function formatTime(time, option) {
if (('' + time).length === 10) {
time = parseInt(time) * 1000
} else {
time = +time
}
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
}
}