最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)的一個日期格式化函數(shù)分享

 更新時間:2014年12月06日 14:24:53   投稿:junjie  
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的一個日期格式化函數(shù)分享,本文給出了實(shí)現(xiàn)代碼和使用例子,需要的朋友可以參考下

由于在工作中,經(jīng)常需要對日期進(jìn)行格式化,不像后端那樣,有方便的方法可調(diào)用,可以在date的對象prototype中定義一個format方法,見如下:

復(fù)制代碼 代碼如下:

// 日期時間原型增加格式化方法
Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];

    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, month);

    str = str.replace(/w|W/g, Week[this.getDay()]);

    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());

    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());

    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
}

調(diào)用的時候比較簡單,比如:

復(fù)制代碼 代碼如下:

var d = new Date();
var str = d.Format("yyyy-MM-dd  hh:mm:ss");
console.log(str);

相關(guān)文章

最新評論

景洪市| 琼中| 水富县| 铜陵市| 朝阳区| 香河县| 栾城县| 广元市| 富蕴县| 赤壁市| 南岸区| 沙湾县| 多伦县| 安顺市| 白沙| 商丘市| 庆阳市| 锡林郭勒盟| 永德县| 乐业县| 雅江县| 南木林县| 伽师县| 房山区| 遂川县| 崇阳县| 连平县| 双辽市| 津南区| 昔阳县| 略阳县| 江津市| 汾西县| 阆中市| 全椒县| 新巴尔虎左旗| 龙口市| 华坪县| 镇赉县| 微山县| 饶阳县|