前端开发超实用的时间函数的封装

前端小程序开发步骤
前端程序开发类
小程序开发后天和前端

html 代码

<script>
    /**
     * 根据时间戳获取年、月、日
     * @param String str 时间戳
     * @param Boolean f 是否在原来的基础上*1000,true要*,false或不填就不*
     * @param num type 显示格式,,不传时默认显示年月日
     */
    function getTimes(str, f, type) {
        var t = f ? parseInt(str) * 1000 : parseInt(str)
        var d = new Date(t)
        var y = d.getFullYear()
        var m = setNum(d.getMonth() + 1)
        var da = setNum(d.getDate())
        var m2 = d.getMonth() + 1
        var h = setNum(d.getHours())
        var mm = setNum(d.getMinutes())
        var ss = setNum(d.getSeconds())
        if (type == 1) {
            return y + ‘ ‘ + m + ‘ ‘ + da + ‘ ‘ + h + ‘ ‘ + mm
        } else if (type == 2) {
            return y + ‘年’ + m + ‘月’ + da + ‘日 ‘ + h + ‘:’ + mm + ‘:’ + ss
        } else if (type == 3) {
            return h + ‘:’ + mm + ‘:’ + ss
        } else if (type == 4) {
            return y + ‘ ‘ + m + ‘ ‘ + da + ‘ ‘ + h + ‘ ‘ + mm
        } else if (type == 5) {
            return y + ‘年’ + m + ‘月’ + da + ‘日 ‘
        } else if (type == 6) {
            return da + ” + m2 + ‘月’
        } else if (type == 7) {
            return y + ‘.’ + m + ‘.’ + da
        } else if (type == 8) {
            return y + ‘-‘ + m + ‘-‘ + da
        } else if (type == 9) {
            return y + m + da + h + mm + ss
        } else if (type == 11) {
            return y + ‘-‘ + m + ‘-‘ + da
        } else if (type == 13) {
            return y + ‘-‘ + m + ‘-‘ + da + ‘ ‘ + h + ‘:’ + mm
        } else if (type == 14) {
            return y
        } else if (type == 15) {
            return m + ‘月’ + da + ‘日’
        } else {
            return y + ‘/’ + m + ‘/’ + da
        }
    }
    /*个位补零*/
    function setNum(s) {
        return parseInt(s) > 9 ? s : ‘0’ + s
    }
    /**
     * 把年月日(2015-10-20)转变为时间戳
     * @param String dateStr 日期
     */
    function getTimeStr(dateStr) {
        var newstr = dateStr.replace(/-/g, ‘/’)
        var date = new Date(newstr)
        var time_str = date.getTime().toString()
        return time_str.substr(0, 10)
    }
    /**
     * 根据年、月、日获取时间戳
     * @param String str 年、月、日
     */
    function stringToTime(stringData) {
        stringData = stringData.replace(/-/g, ‘/’)
        var date = new Date(stringData)
        var time = date.getTime()
        return time
    }
</script>

还有一种比较常用的时间用法就是获取时间差!
html 代码

<script>
    /* 计算时间差
     * @param Long createTime 时间戳
     */
    function getDateDiff(time) {
        var result1 = ”
        var str2 = time
        var minute = 1000 * 60
        var hour = minute * 60
        var day = hour * 24
        var halfamonth = day * 15
        var month = day * 30
        var now = new Date().getTime()
        var diffValue = now – str2
        var monthC = diffValue / month
        var weekC = diffValue / (7 * day)
        var dayC = diffValue / day
        var hourC = diffValue / hour
        var minC = diffValue / minute
        if (monthC >= 1) {
            result1 = getTimes(str2, false, 15)
        } else if (weekC >= 1) {
            result1 = parseInt(weekC) + ‘周前’
        } else if (dayC >= 1) {
            result1 = parseInt(dayC) + ‘天前’
        } else if (hourC >= 1) {
            result1 = parseInt(hourC) + ‘个小时前’
        } else if (minC >= 1) {
            result1 = parseInt(minC) + ‘分钟前’
        } else result1 = ‘刚刚发表’
        return result1
    }
</script>
程序开发前端
前端 之小程序开发
前端小程序开发
» 本文来自:前端开发者 » 《前端开发超实用的时间函数的封装》
» 本文链接地址:https://www.rokub.com/7225.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!