转换时间格式
//原始时间格式:Thu Aug 18 20:38:54 CST 2016
//转换时间格式:2016-08-19 10:38
export function getTaskTime(strDate) {
var date = new Date(strDate);
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();
minute = minute < 10 ? ('0' + minute) : minute;
var seconds = date.getSeconds();
seconds = seconds < 10 ? ('0' + seconds) : seconds;
var str = y+"-"+m+"-"+d+" "+h+":"+minute+":"+seconds;
return str;
}
时间戳格式转换
// 时间戳 格式化
export function formatDate(n, detailed) {
if (isNaN(parseFloat(n, 10)) || isNaN(n)) return '-';
let o = new Date(parseInt(n, 10));
let y = o.getFullYear(),
d = o.getDate() < 10 ? '0' + o.getDate() : o.getDate(),
m = (o.getMonth() + 1) < 10 ? '0' + (o.getMonth() + 1) : (o.getMonth() + 1),
h = o.getHours() < 10 ? '0' + o.getHours() : o.getHours(),
s = o.getMinutes() < 10 ? '0' + o.getMinutes() : o.getMinutes(),
e = o.getSeconds() < 10 ? '0' + o.getSeconds() : o.getSeconds();
if(detailed == 'seconds'){
return '' + y + '-' + m + '-' + d + ' ' + h + ':' + s + ':' + e
}
return !!detailed ? '' + y + '-' + m + '-' + d + ' ' + h + ':' + s : '' + y + '-' + m + '-' + d;
}
json 中只保留指定key
export function saveField(json, fields) {
for (let i in json) { if (!(fields.indexOf(i) >= 0)) delete json[i] }
}
转 utf8 编码
export function coding2utf8(str) {
if (str === '' || str == null ) return '-';
try {
return JSON.stringify(JSON.parse(str));
}
catch(e) {
return str;
}
}
移除对象中的空属性
export function removeEmpty(obj) {
let o = {};
for (let k in obj) {
let v = obj[k];
if (v === false || v === 0) {
o[k] = v;
} else if (v) {
o[k] = v;
}
}
return o;
}