在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

javaScript日期格式化

JavaScript 清风 314℃ 0评论

/*
var d=new Date();
console.log(d.toString());    //2012-7-27 9:26:52
console.log(d.toString(""));    //2012-7-27 9:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss"));    //2012-07-27 09:26:52
console.log(d.toString("yyyy年MM月dd日 HH:mm:ss"));    //2012年07月27日 09:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss fff"));    //2012-07-27 09:26:52 237
console.log(d.toString("yyyy年 MMM dd EEE"));    //2012年 七月 27 星期五
console.log(d.toString("yyyy MMM dd EEE","en"));    //2012 Jul 27 Fri
*/
Date.prototype.toString = function(format, loc) {
    var time = {};
    time.Year = this.getFullYear();
    time.TYear = ("" + time.Year).substr(2);
    time.Month = this.getMonth() + 1;
    time.TMonth = time.Month < 10 ? "0" + time.Month: time.Month;
    time.Day = this.getDate();
    time.TDay = time.Day < 10 ? "0" + time.Day: time.Day;
    time.Hour = this.getHours();
    time.THour = time.Hour < 10 ? "0" + time.Hour: time.Hour;
    time.hour = time.Hour < 13 ? time.Hour: time.Hour - 12;
    time.Thour = time.hour < 10 ? "0" + time.hour: time.hour;
    time.Minute = this.getMinutes();
    time.TMinute = time.Minute < 10 ? "0" + time.Minute: time.Minute;
    time.Second = this.getSeconds();
    time.TSecond = time.Second < 10 ? "0" + time.Second: time.Second;
    time.Millisecond = this.getMilliseconds();
    time.Week = this.getDay();

    var MMMArrEn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var MMMArr = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
    var WeekArrEn = ["Sun", "Mon", "Tue", "Web", "Thu", "Fri", "Sat"];
    var WeekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

    var oNumber = time.Millisecond / 1000;

    if (format != undefined && format.replace(/\s/g, "").length > 0) {
        if (loc != undefined && loc == "en") {
            MMMArr = MMMArrEn.slice(0);
            WeekArr = WeekArrEn.slice(0);
        }
        format = format.replace(/yyyy/ig, time.Year).replace(/yyy/ig, time.Year).replace(/yy/ig, time.TYear).replace(/y/ig, time.TYear).replace(/MMM/g, MMMArr[time.Month - 1]).replace(/MM/g, time.TMonth).replace(/M/g, time.Month).replace(/dd/ig, time.TDay).replace(/d/ig, time.Day).replace(/HH/g, time.THour).replace(/H/g, time.Hour).replace(/hh/g, time.Thour).replace(/h/g, time.hour).replace(/mm/g, time.TMinute).replace(/m/g, time.Minute).replace(/ss/ig, time.TSecond).replace(/s/ig, time.Second).replace(/fff/ig, time.Millisecond).replace(/ff/ig, oNumber.toFixed(2) * 100).replace(/f/ig, oNumber.toFixed(1) * 10).replace(/EEE/g, WeekArr[time.Week]);
    } else {
        format = time.Year + "-" + time.Month + "-" + time.Day + " " + time.Hour + ":" + time.Minute + ":" + time.Second;
    }
    return format;
}

/*
var time1 = new Date("2012/01/01 00:00:00 UTC+800");
var str1 = time1.format("yyyy年MM月dd日 H时mm分ss秒 TT")
console.log("Date["+time1+"]");
console.log("格式化结果为:" + str1);
 
var str2 = "2012年1月23日";
var time2 = Date.parseFormat(str2, "yyyy年M月dd日");
console.log("时间字符串:"+str2);
console.log("格式化结果: Date["+time2+"]");
*/
Date.format = function(d, mask) {
    var zeroize = function(value, length) {
        if (!length) length = 2;
        value = String(value);
        for (var i = 0,
        zeros = ''; i < (length - value.length); i++) {
            zeros += '0';
        }
        return zeros + value;
    };
    return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g,
    function($0) {
        switch ($0) {
        case 'd':
            return d.getDate();
        case 'dd':
            return zeroize(d.getDate());
        case 'ddd':
            return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()];
        case 'dddd':
            return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()];
        case 'M':
            return d.getMonth() + 1;
        case 'MM':
            return zeroize(d.getMonth() + 1);
        case 'MMM':
            return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()];
        case 'MMMM':
            return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()];
        case 'yy':
            return String(d.getFullYear()).substr(2);
        case 'yyyy':
            return d.getFullYear();
        case 'h':
            return d.getHours() % 12 || 12;
        case 'hh':
            return zeroize(d.getHours() % 12 || 12);
        case 'H':
            return d.getHours();
        case 'HH':
            return zeroize(d.getHours());
        case 'm':
            return d.getMinutes();
        case 'mm':
            return zeroize(d.getMinutes());
        case 's':
            return d.getSeconds();
        case 'ss':
            return zeroize(d.getSeconds());
        case 'l':
            return zeroize(d.getMilliseconds(), 3);
        case 'L':
            var m = d.getMilliseconds();
            if (m > 99) m = Math.round(m / 10);
            return zeroize(m);
        case 'tt':
            return d.getHours() < 12 ? 'am': 'pm';
        case 'TT':
            return d.getHours() < 12 ? 'AM': 'PM';
        case 'Z':
            return d.toUTCString().match(/[A-Z]+$/);
            // Return quoted strings with the surrounding quotes removed
        default:
            return $0.substr(1, $0.length - 2);
        }
    });
};
Date.prototype.format = function(mask) {
    return Date.format(this, mask);
};
Date.parseFormat = function(str, format) {
    var pattern = format.replace(/(yyyy)/g, "([0-9]{4})").replace(/(yy)|(MM)|(dd)|(hh)|(mm)|(ss)/g, "([0-9]{2})").replace(/[Mdhms]/g, "([0-9]{1,2})");
    var getIndex = function(expr1, expr2) {
        var index = format.indexOf(expr1);
        if (index == -1) index = format.indexOf(expr2);
        return index;
    }
    var returnDate;
    if (new RegExp(pattern).test(str)) {
        var yPos = getIndex("yyyy", "yy");
        var mPos = getIndex("MM", "M");
        var dPos = getIndex("dd", "d");
        var hPos = getIndex("hh", "h");
        var miPos = getIndex("mm", "m");
        var sPos = getIndex("ss", "s");
        var data = {
            y: 0,
            m: 0,
            d: 0,
            h: 0,
            mi: 0,
            s: 0
        };
        var pos = [yPos + ",y", mPos + ",m", dPos + ",d", hPos + ",h", miPos + ",mi", sPos + ",s"].sort(function(a, b) {
            a = parseInt(a.split(',')[0]);
            b = parseInt(b.split(',')[0]);
            return a > b;
        });
        var tmpIndex = 0;
        var newPos = [];
        for (var i = 0,
        l = pos.length; i < l; i++) {
            if (parseInt(pos[i].split(',')[0]) != -1) {
                newPos[tmpIndex] = pos[i];
                tmpIndex++;
            }
        }
        var m = str.match(pattern);
        for (var i = 1,
        l = m.length; i < l; i++) {
            if (i == 0) break;
            var flag = newPos[i - 1].split(',')[1];
            data[flag] = m[i];
        };
        data.y = data.y || today.getFullYear();
        data.d = data.d || today.getDate();
        if (data.y.toString().length == 2) data.y = parseInt('20' + data.y);
        data.m -= 1;
        returnDate = new Date(data.y, data.m, data.d, data.h, data.mi, data.s);
    }
    return returnDate;
};

转载请注明:清风博客 » javaScript日期格式化

喜欢 (0)or分享 (0)
支付宝扫码打赏 微信打赏
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址