秒钟转换为 00:00:00

// new Date()=====Fri Mar 30 2018 09:31:03 GMT+0800 (中国标准时间)

// 变成 2018/ 03 / 30 09:31:03
const formatTime = date => {

  const year = date.getFullYear()

  const month = date.getMonth() + 1

  const day = date.getDate()

  const hour = date.getHours()

  const minute = date.getMinutes()

  const second = 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

}



// 时间由秒变成00:00:00

const shijian= function (t) {

    var NowtimeValue = t;

    var nowH = parseInt(NowtimeValue / 3600);

    var nowM = parseInt(NowtimeValue % 3600 / 60);

    var nowS = parseInt(NowtimeValue % 60);

    nowH < 10 ? nowH = "0" + nowH : nowH = nowH;

    nowM < 10 ? nowM = "0" + nowM : nowM = nowM;

    nowS < 10 ? nowS = "0" + nowS : nowS = nowS;

    return nowH + ":" + nowM + ":" + nowS

}



var util = {
    /**
     * 获取url参数.
     * @param {String} [name] 参数名称,无此参数时返回所有参数
     * @return {String|Object} name存在时返回相应的值,否则返回所有参数
     */
    getUrlParam: function(name) {
        var url = window.location.search.substr(1);
        if (!url) {
            return '';
        }
        if (name) {
            var value = new RegExp('(?:^|&)' + name + '=([^&]*)(&|$)', 'g').exec(url);
            return util.htmlEncodeAll(value && window.decodeURIComponent(value[1]) || '');
        }
        var result = {};
        var reg = /(?:^|&)([^&=]+)=([^&]*)(?=(&|$))/g;
        var item;
        /* jshint boss: true */
        while (item = reg.exec(url)) {
            result[item[1]] = util.htmlEncodeAll(window.decodeURIComponent(item[2]));
        }
        return result;
    },
    /**
     * 过滤html中的特殊符号
     * @param  {String} [e] 待过滤的html
     * @return {String}  返回过滤后的html
     */
    htmlEncodeAll: function(e) {
        return null == e ? "" : e.replace(/\/g, ">").replace(/\&/g, "&").replace(/"/g, """).replace(/'/g, "'");
    },
    /**
     * 添加script.
     * @param {String} url js url
     * @param {Function} [onload] 加载成功回调
     * @param {Function} [onerror] 加载失败回调
     * @return {HTMLElement} script引用
     */
    addScript: function(url, onload, onerror) {
        var script = document.createElement('script');
        if (onload) {
            script.onload = function() {
                onload(script);
            };
        }
        script.onerror = function() {
            if(onerror){
                onerror(script);
            }else if(onload){
                onload(script);
            }
        };
        script.src = url;
        document.head.appendChild(script);
        return script;
    },
    /**
     * 复制对象属性.
     * @param {Object} toObj 复制到此对象
     * @param {Object} fromObj 要复制的对象
     */
    extend: function(toObj, fromObj) {
        for (var key in fromObj) {
            if (fromObj[key] !== 'undefined') {
                toObj[key] = fromObj[key];
            }
        }
    },
    /**
     * 为url添加变量.
     * @param {String} url
     * @param {String|Object} name
     *    为字符串类型时参数作为新增参数的名称,第三个参数不能缺省
     *    为对象类型时参数为要增加的参数集合,属性为参数名称,值为参数值
     * @param {String} value 变量值
     * @return {String} 新的url
     */
    urlAddParam: function(url, name, value) {
        // 分割url,arr[1] 为头部,arr[2]为参数,arr[3]为hash
        var arr = url.match(/([^\?#]*\??)([^#]*)?(#.*)?/);
        var prefix = arr[1];
        var param = arr[2];

        if (param) {
            prefix += param + '&';
        } else if (arr[1].indexOf('?') === -1) {
            prefix += '?';
        }
        var newParam = '';
        if (typeof name === 'object') {
            for (var key in name) {
                newParam += '&' + key + '=' + encodeURIComponent(name[key]);
            }
            newParam = newParam.substr(1);
        } else {
            newParam = name + '=' + encodeURIComponent(value);
        }
        return prefix + newParam + (arr[3] || '');
    },
    // Check if documentElement already has a given class.
    hasClass: function(node, className) {
        var regex;
        regex = new RegExp(className, 'i');
        return node.className.match(regex);
    },

    // Add one or more CSS classes to the  element.
    addClass: function(node, className) {
        var currentClassNames = null;
        if (!this.hasClass(node, className)) {
            currentClassNames = node.className.replace(/^\s+|\s+$/g, '');
            node.className = currentClassNames + " " + className;
        }
    },

    // Remove single CSS class from the  element.
    removeClass: function(node, className) {
        if (this.hasClass(node, className)) {
            node.className = node.className.replace(" " + className, "");
        }
    }
};

module.exports = util;

你可能感兴趣的:(秒钟转换为 00:00:00)