// 給String對(duì)象添加getDate方法枕稀,使字符串形式的日期返回為Date型的日期
String.prototype.getDate = function(){
var strArr = this.split('-');
var date = new Date(strArr[0], strArr[1] - 1, strArr[2]);
return date;
}
javascript中將Date類型數(shù)據(jù)裝換為··前
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小時(shí)
"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;
};
//時(shí)間轉(zhuǎn)化
Date.prototype.toRelativeTime = function(now_threshold) {
var delta = new Date() - this;
now_threshold = parseInt(now_threshold, 10);
if (isNaN(now_threshold)) {
now_threshold = 0;
}
if (delta <= now_threshold) {
return '剛剛';
}
var units = null;
var conversions = {
'毫秒': 1, // ms -> ms
'秒': 1000, // ms -> sec
'分鐘': 60, // sec -> min
'小時(shí)': 60, // min -> hour
'天': 24, // hour -> day
'月': 30, // day -> month (roughly)
'年': 12 // month -> year
};
for (var key in conversions) {
if (delta < conversions[key]) {
break;
} else {
units = key; // keeps track of the selected key over the iteration
delta = delta / conversions[key];
}
}
// pluralize a unit when the difference is greater than 1.
delta = Math.floor(delta);
return [delta, units].join(" ");
};