JavaScript 时间格式

方法1:

Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + , //月份
"d+": this.getDate(), //日
"h+": this.getHours()%==?:this.getHours()%, //小时
"H+": this.getHours(),
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + ) / ), //季度
"f": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$, (this.getFullYear() + "").substr( - RegExp.$.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
var time3 = new Date().Format("yyyy-MM-dd hh:mm:ss:f");
var time4=new Date().toLocaleString();
console.log(time1);
console.log(time2);
console.log(time3);
console.log(time4);

JavaScript 时间格式

方法2:

Date.prototype.format = function(formatStr)
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六']; str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % )>?(this.getYear() % ).toString():'' + (this.getYear() % ));
//
this.setMonth(this.getMonth()+);
str=str.replace(/MM/,this.getMonth()>?this.getMonth().toString():'' + this.getMonth());
str=str.replace(/M/g,this.getMonth()); str=str.replace(/w|W/g,Week[this.getDay()]); str=str.replace(/dd|DD/,this.getDate()>?this.getDate().toString():'' + this.getDate());
str=str.replace(/d|D/g,this.getDate()); str=str.replace(/hh|HH/,this.getHours()>?this.getHours().toString():'' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>?this.getMinutes().toString():'' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes()); str=str.replace(/ss|SS/,this.getSeconds()>?this.getSeconds().toString():'' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds()); str=str.replace(/f/,this.getMilliseconds()); return str;
}

跟上面的一样调用

但是小时没有12小时制的了

http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

上一篇:百度贴吧爬虫


下一篇:力扣--合并两个有序数组