您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

时间处理,空间动态1分钟前,昨天,2天前

2026/3/24 13:07:32发布13次查看
时间处理类库,可以传入时间戳。自己写的一个时间处理,如手机通讯的1分钟前,2小时前,昨天,3天前,4月5日
自己写的一个时间处理,如手机通讯的1分钟前,2小时前,昨天,3天前,4月5日
可以传入时间戳
demo 演示 http://mall.hnsanx.com/yzzs/dataclass/date.html
关键代码:
var now = new date(); //获得当前系统时间
date.prototype.format = function(format){
var o = {
m+ : this.getmonth()+1, //month
d+ : this.getdate(), //day
h+ : this.gethours(), //hour
m+ : this.getminutes(), //minute
s+ : this.getseconds(), //second
q+ : math.floor((this.getmonth()+3)/3), //quarter
s : this.getmilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(regexp.$1, (this.getfullyear()+).substr(4 - regexp.$1.length));
}
for(var k in o) {
if(new regexp((+ k +)).test(format)) {
format = format.replace(regexp.$1, regexp.$1.length==1 ? o[k] : (00+ o[k]).substr((+ o[k]).length));
}
}
return format;
}
var dateclass = {
//===============================
// 转化时间戳
// dateclass.transdate(2015-03-30 14:16:32);
// 2015-03-30
//===============================
transdate : function(endtime){
var date=new date();
date.setfullyear(endtime.substring(0,4));
date.setmonth(endtime.substring(5,7)-1);
date.setdate(endtime.substring(8,10));
date.sethours(endtime.substring(11,13));
date.setminutes(endtime.substring(14,16));
date.setseconds(endtime.substring(17,19));
return date.parse(date)/1000;
},
//===============================
// 时间格式化输出 获得当前时间
// dateclass.formatdate(1230999938,'yyyy-mm-dd hh:mm:ss'); 1230999938为时间戳
// 2015-03-30
//===============================
formatdate : function(strtime,format) {
var format = format || 'yyyy-mm-dd hh:mm:ss';
var d = strtime ? new date(strtime) : new date();
return d.format(format);
},
//===============================
// 爬虫时间格式化
// dateclass.formatdate(yyyy-mm-dd h:i:s);
// 2015-03-30
//===============================
dateprocess : function(olddate) {
//alert(isnan(olddate));
if(!isnan(olddate)) odateseconds = olddate;
else{
var odate = this.formatdate(olddate) //时间标准格式化
var odateseconds = this.transdate(odate);
}
//var odate = this.formatdate(olddate)
var nowstr = now.format('yyyy-mm-dd hh:mm:ss'); //系统当前时间格式化
var nowseconds = this.transdate(nowstr);
//alert(nowseconds+和+odateseconds);
if(nowseconds > odateseconds) {
var toseconds = nowseconds - odateseconds;
if( toseconds if( toseconds if(toseconds if(toseconds if(toseconds if(toseconds return toseconds + 秒前; //59秒前
}else{
return math.floor(toseconds/60) + 分钟前 ; //59分钟前
}
}else{
return math.floor(toseconds/3600) + 小时前; //23小时前
}
}else{
var nowhour = now.gethours();
if(toseconds>(nowhour+24)*3600) return 1天前;
else return 昨天;//昨天
}
}else {
return math.floor(toseconds/86400) + 天前; //6天前
}
}
else {
return (new date(olddate)).format(m月d日); //输出12月12日
}
}else {
//alert(toseconds);
return (new date(olddate)).format(yyyy年m月d日) //输出yyyy-mm-dd
}
}else{
return 时间超前; //待处理时间为过去的时间
}
}
}
dateclass.rar ( 1.28 kb 下载:21 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product