zoukankan      html  css  js  c++  java
  • Javascript扩展Date的prototype实现时间format函数 2017-06-29T09:10:00.000Z日期转换

    /*时间格式化 公用方法*/
    Date.prototype.format = function(fmt) { //
        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), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    var myDateStr = new Date(‘2017-06-29T09:10:00.000Z’).format('yyyy-MM-dd hh:mm:ss')
    
    //计算时间间隔,如:刚刚,两小时前,1年前
    Date.prototype.comparTime = function(in_date) {
        var timestamp = Date.parse(new Date());
        var d_value = Math.ceil((timestamp - in_date) / 1000);
        var d_html = "";
        if (d_value < 60) {
            d_html = "1分钟内";
        } else if (d_value > 60 && d_value < 3600) {
            d_html = parseInt(d_value / 60) + "分钟前";
        } else if (d_value > 3600 && d_value < 86400) {
            d_html = parseInt(d_value / 3600) + "小时前";
        } else if (d_value > 86400 && d_value < 604800) {
            d_html = parseInt(d_value / 86400) + "天前";
        } else if (d_value > 604800 && d_value < 2419200) {
            d_html = parseInt(d_value / 604800) + "周前";
        } else if (d_value > 2419200 && d_value < 31536000) {
            d_html = parseInt(d_value / 2419200) + "月前";
        } else if (d_value > 31536000) {
            d_html = parseInt(d_value / 31536000) + "年前";
        }
        return d_html;
    };
  • 相关阅读:
    Codeforces 1265A Beautiful String
    1039 Course List for Student (25)
    1038 Recover the Smallest Number (30)
    1037 Magic Coupon (25)
    1024 Palindromic Number (25)
    1051 Pop Sequence (25)
    1019 General Palindromic Number (20)
    1031 Hello World for U (20)
    1012 The Best Rank (25)
    1011 World Cup Betting (20)
  • 原文地址:https://www.cnblogs.com/mxyr/p/9238343.html
Copyright © 2011-2022 走看看