亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript格式化日期時(shí)間的方法和自定義格式化函數(shù)示例

 更新時(shí)間:2014年04月04日 16:21:35   作者:  
JavaScript默認(rèn)的時(shí)間格式我們一般情況下不會(huì)用,所以需要進(jìn)行格式化,下面說說我總結(jié)的JavaScript時(shí)間格式化方法

很多時(shí)候,我們可以利用JavaScript中Date對(duì)象的內(nèi)置方法來格式化,如:

復(fù)制代碼 代碼如下:
var d = new Date();
console.log(d); // 輸出:Mon Nov 04 2013 21:50:33 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
console.log(d.toDateString()); // 日期字符串,輸出:Mon Nov 04 2013
console.log(d.toGMTString()); // 格林威治時(shí)間,輸出:Mon, 04 Nov 2013 14:03:05 GMT
console.log(d.toISOString()); // 國際標(biāo)準(zhǔn)組織(ISO)格式,輸出:2013-11-04T14:03:05.420Z
console.log(d.toJSON()); // 輸出:2013-11-04T14:03:05.420Z
console.log(d.toLocaleDateString()); // 轉(zhuǎn)換為本地日期格式,視環(huán)境而定,輸出:2013年11月4日
console.log(d.toLocaleString()); // 轉(zhuǎn)換為本地日期和時(shí)間格式,視環(huán)境而定,輸出:2013年11月4日 下午10:03:05
console.log(d.toLocaleTimeString()); // 轉(zhuǎn)換為本地時(shí)間格式,視環(huán)境而定,輸出:下午10:03:05
console.log(d.toString()); // 轉(zhuǎn)換為字符串,輸出:Mon Nov 04 2013 22:03:05 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
console.log(d.toTimeString()); // 轉(zhuǎn)換為時(shí)間字符串,輸出:22:03:05 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
console.log(d.toUTCString()); // 轉(zhuǎn)換為世界時(shí)間,輸出:Mon, 04 Nov 2013 14:03:05 GMT

如果上面的方法不能滿足我們的要求,也可以自定義函數(shù)來格式化時(shí)間,如:
復(fù)制代碼 代碼如下:

Date.prototype.format = function(format) {
       var date = {
              "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+)/i.test(format)) {
              format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
       }
       for (var k in date) {
              if (new RegExp("(" + k + ")").test(format)) {
                     format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
              }
       }
       return format;
}
var d = new Date().format('yyyy-MM-dd');
console.log(d); // 2013-11-04

 

相關(guān)文章

最新評(píng)論