1. 程式人生 > >javascript常用(實用)工具類

javascript常用(實用)工具類

// 時間格式化
const formatTime = date => { const year = new Date(date).getFullYear() const month = new Date(date).getMonth() + 1 const day = new Date(date).getDate() const hour = new Date(date).getHours() const minute = new Date(date).getMinutes() const second = new Date(date).getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } /** * 用於post 引數格式轉換 */ function json2Form(json) { var str = []; for (var p in json) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p])); } return str.join("&"); } // 物件陣列去重 Array.prototype.filter = function () { let emptyObj = {} //建立空物件,用於存放屬性 let result = [] //結果陣列,用於存放結果 for (let i = 0; i < this.length; i++) {//this指向呼叫該方法的陣列 this[i] = JSON.stringify(this[i])//將物件解析為字串便於比較 if (!emptyObj[this[i]]) {//以emptyObj是否存在該字串為名的屬性進行判斷 emptyObj[this[i]] = 1 //將emptyObj的this[i]屬性設為1,便於判斷 result.push(JSON.parse(this[i]))//將字串重新解析為物件 } } return result //返回結果 } // 價格過濾 function currency(value, currency, decimals) { const digitsRE = /(\d{3})(?=\d)/g value = parseFloat(value) if (!isFinite(value) || (!value && value !== 0)) return '' currency = currency != null ? currency : '$' decimals = decimals != null ? decimals : 2 var stringified = Math.abs(value).toFixed(decimals) var _int = decimals ? stringified.slice(0, -1 - decimals) : stringified var i = _int.length % 3 var head = i > 0 ? (_int.slice(0, i) + (_int.length > 3 ? ',' : '')) : '' var _float = decimals ? stringified.slice(-1 - decimals) : '' var sign = value < 0 ? '-' : '' return sign + currency + head + _int.slice(i).replace(digitsRE, '$1,') + _float } /** * 時間戳轉化為年 月 日 時 分 秒 * number: 傳入時間戳 * format:返回格式,支援自定義,但引數必須與formateArr裡保持一致 */ function formatDatebox(value, parent) { if (value == null || value == '') { return ''; } var dt = parseToDate(value);// 關鍵程式碼,將那個長字串的日期值轉換成正常的JS日期格式 return dt.format(parent); // 這裡用到一個javascript的Date型別的拓展方法,這個是自己新增的拓展方法,在後面的步驟3定義 } function parseToDate(value) { if (value == null || value == '') { return undefined; } var dt; if (value instanceof Date) { dt = value; } else { if (!isNaN(value)) { dt = new Date(value); } else if (value.indexOf('/Date') > -1) { value = value.replace(/\/Date(−?\d+)\//, '$1'); dt = new Date(); dt.setTime(value); } else if (value.indexOf('/') > -1) { dt = new Date(Date.parse(value.replace(/-/g, '/'))); } else { dt = new Date(value); } } return dt; } // 為Date型別拓展一個format方法,用於格式化日期 Date.prototype.format = function (format) // author: meizz { var o = { "M+": this.getMonth() + 1, // month "d+": this.getDate(), // day "h+": this.getHours(), // hour "m+": this.getMinutes(), // minute "s+": this.getSeconds(), // second "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() // millisecond }; if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; }; //兩日期相減獲得天數 function num_data(startDate,endDate) { var end_date = new Date(endDate.replace(/-/g, "/")); var start_date = new Date(startDate.replace(/-/g, "/")); var days = end_date.getTime() - start_date.getTime(); var day = parseInt(days / (1000 * 60 * 60 * 24)); return day; } module.exports = { formatTime, json2Form, formatDatebox, num_data }