zoukankan      html  css  js  c++  java
  • util-tool工具 、时间格式化

    
    
    
    
    
    //UTC 转指定格式日期
    let date = '2018-03-07T16:00:00.000Z'
    console.log(moment(date).format('YYYY-MM-DD HH:mm:ss'))

    //GMT
    let localTime = moment.utc(date).toDate();
    console.log(localTime)

    //本地日期转UTC
    console.log(new Date("2018-03-08 00:00:00").toISOString())



    /*
    * * * @description: 格式化日期 * @Date: 2019/10/16 09:32 * */ Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), // "H+": this.getHours(), //小时 "m+": this.getMinutes(), // "s+": this.getSeconds(), // "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "f+": this.getMilliseconds() //毫秒 }; 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; } export default Date.prototype.format 然后我们使用其格式日期 require(Date); // const now = new Date().format("yyyy/MM/dd HH:mm:ss.S"); const now = new Date().format("yyyy/MM/dd HH:mm:ss");

    2、遍历对象及数组

    /**
     * each 遍历数组及对象
     * @param obj {Object|Array} 遍历对象
     * @param callback {Function} 回调函数,第一个参数为val,第二个为key,这里与jquery相反
     */
    each(obj: Array < any > , callback: Function) : Array < any > {
      let length: number,
      i: number | string = 0;
    
      if (isArrayLike(obj)) {
        length = obj.length;
        for (; i < length; i++) {
          if (callback.call(obj[i], obj[i], i) === false) {
            break;
          }
        }
      } else {
        for (i in obj) {
          if (callback.call(obj[i], obj[i], i) === false) {
            break;
          }
        }
      }
    
      return obj;
    }
    使用示例:(tks是文件名称)
    tks.each({ aa: 1, bb: 2, cc: 3 },(v, i) = >{
      console.log(v, i);
    })
    tks.each([1, 3, 5, 7, 9], (v, i) = >{
      console.log(v, i);
    })
    
    
    3、格式校验:

    let myTool = { // 手机号验证 checkPhone(str) { return /^1[0-9]{10}$/.test(str) }, // 电话验证 tel(str) { return /^(0d{2,3}-d{7,8})(-d{1,4})?$/.test(str) }, // 首尾空格去除 trim(str) { return str.replace(/(^s*)|(s*$)/g, '') }, // 替换所有空格 trimAll(str) { return str.replace(/s+/g, ''); }, // 获取url参数 getQueryString(name) { let url = location.href.split('?')[1] let theRequest = {} if (url) { let strs = url.split('&') for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split('=')[0]] = decodeURIComponent(strs[i].split('=')[1]) } } return theRequest[name] }, // 设置cookie setCookie(name, value, iDay) { var oDate = new Date() oDate.setDate(oDate.getDate() + iDay) document.cookie = name + '=' + value + ';expires=' + oDate + '; path=/' }, // 获取cookie getCookie(name) { var arr = document.cookie.split('; ') for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split('=') if (arr2[0] === name) { return arr2[1] } } return '' }, // 删除cookie removeCookie(name) { this.setCookie(name, '', -1) }, // 设置本地存储 setItem(name, value) { localStorage.setItem(key, JSON.stringify({ val: value, time: new Date().getTime() })) }, // 获取本地存储 getItem(key, day) { let text = '' let val = localStorage.getItem(key) if (val) { let item = JSON.parse(val) if (!item.time || (new Date().getTime() - item.time > day * 24 * 60 * 60 * 1e3)) { localStorage.removeItem(key) } else { text = item.val } } return text }, // 获取文本长度 getStringLen: function (str) { var len = 0; for (var i = 0; i < str.length; i++) { str.charCodeAt(i) > 255 ? len += 2 : len += 1; } return len; }, // 判断微信浏览器 isWeiXin: /micromessenger/i.test(navigator.userAgent.toLowerCase()), isIos: /iphone|ipad/i.test(navigator.userAgent.toLowerCase()), isAndroid: /android/i.test(navigator.userAgent.toLowerCase()), // 判断数据类型 isType(obj, type) { // obj instanceof Array // obj.constructor === Array // Array.isArray(obj) // undefined function boolean number string array object return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() === type }, // 深拷贝对象 clone(obj) { if ("object" != typeof obj) return obj var copy = obj instanceof Array ? [] : {} for (var attr in obj) { copy[attr] = clone(obj[attr]) } return copy }, // 时间戳转时间 timeFormat(val, type) { let time = '' if (val) { let date = new Date(val * 1000) 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 h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() if (type === 'second') { // 2018.11.12 00:00 time = `${year}.${month}.${day} ${h}:${m}` } else if (type === 'time') { // 12月11日00:00 time = `${month}月${day}日${h}:${m}` } else if (type === 'month') { // 12月11日 time = `${month}月${day}日` } } return time }, // 数组里最大的数字 max(arr) { // 方法1 return Math.max(...arr) // 方法2 return Math.max.apply(Math, arr) }, // 设置html的font-size setFontSize(_client) { // _client 设计稿的宽度 let docEl = document.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', countSize = function () { // 方法1 let devieWidth = Math.min(_client, docEl.clientWidth, docEl.clientHeight) let fonSize = devieWidth > 1080 ? 144 : devieWidth / _client * 100 docEl.style.fontSize = fonSize + 'px' // 方法2 根据vw ios兼容最低7 android兼容最低4.4 docEl.style.fontSize = 100 / _client * 100 + 'vw' }; window.addEventListener(resizeEvt, countSize, false) countSize() }, // 查找类名 html5属性classList hasClass(obj, className) { // let obj = document.querySelectorAll(obj) return obj.classList.contains(className) }, // 添加类名 addClass(obj, className) { obj.classList.add(className) return this }, // 删除类名 removeClass(obj, className) { obj.classList.remove(className) return this }, // 动画 animate(obj, josn, fn) { function css(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr] } return getComputedStyle(obj, false)[attr] } clearInterval(obj.iTimer) var iSpeed = 0 var iCur = 0 obj.iTimer = setInterval(function () { var iEnd = true for (var attr in json) { iCur = parseInt(css(obj, attr)) iSpeed = (json[attr] - iCur) / 8 iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed) if (iCur !== json[attr]) { iEnd = false; obj.style[attr] = iSpeed + iCur + 'px'; } if (iEnd) { clearInterval(obj.iTimer); fn && fn.call(obj); } } }, 30); }, // 拖拽移动 drag(block) { var oW var oH var maxWidth = document.documentElement.clientWidth - block.offsetWidth var maxHeight = document.documentElement.clientHeight - block.offsetHeight block.addEventListener('touchstart', function (e) { var touches = e.touches[0] oW = touches.clientX - block.offsetLeft oH = touches.clientY - block.offsetTop document.addEventListener('touchmove', defaultEvent, false) }, false) block.addEventListener('touchmove', function (e) { var touches = e.touches[0] var oLeft = touches.clientX - oW var oTop = touches.clientY - oH oLeft = oLeft < 0 ? 0 : oLeft oLeft = oLeft > maxWidth ? maxWidth : oLeft oTop = oTop < 0 ? 0 : oTop oTop = oTop > maxHeight ? maxHeight : oTop block.style.left = oLeft + 'px' block.style.top = oTop + 'px' e.preventDefault() }, false) block.addEventListener('touchend', function () { document.removeEventListener('touchmove', defaultEvent, false) }, false) }, // ajax封装 ajax(opt) { /* opt:参数 method: 请求方式 url: 请求url data: 请求参数 callback:请求成功回调 async: 请求异步同步,默认异步 */ let oAjax = null let j = {} if (window.XMLHttpRequest) { oAjax = new XMLHttpRequest(); } else { oAjax = new ActiveXObject('Microsoft.XMLHTTP') } j.method = opt.method || 'get' j.url = opt.url || '' j.data = opt.data || '' j.callback = opt.callback || function () { } if (j.method == 'get' && j.data) { let data_send = '?' Object.keys(j.data).forEach(key => { data_send += `${key}=${j.data[key]}` }) j.url += data_send.slice(0, -1) } oAjax.open(j.method, j.url, opt.async || true) if (j.method == 'get') { oAjax.send() } else { let data = j.data let header = 'application/x-www-form-urlencoded' if (typeof j.data !== 'string') { header = 'application/json' data = JSON.stringify(data) } xhr.setRequestHeader('Content-type', header) xhr.send(data) } oAjax.onreadystatechange = function () { if (oAjax.readyState == 4) { if (oAjax.status == 200) { j.callback(oAjax.responseText) } } } } }
    sunshine15666
  • 相关阅读:
    grid列的值格式化
    页面记载给绑定query的grid加filter
    页面加载后从后面带数据到前台
    waf2控件名
    通讯框架选型
    C# 访问修饰符和const、readonly
    ZooKeeper典型应用场景一览
    ZooKeeper典型使用场景一览
    摘的一段关于原型的介绍
    D3.js和three.js
  • 原文地址:https://www.cnblogs.com/xiaolucky/p/11586083.html
Copyright © 2011-2022 走看看