人类可读的时间javascript/jquery
我在John Resig的博客上发现了以下片段:人类可读的时间javascript/jquery,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我在John Resig的博客上发现了以下片段: function prettyDate(time){ var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), diff = (((new Date()).getTime() - date.getTime()) / 1000), day_diff = Math.floor(diff / 86400); if
function prettyDate(time){
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 min" ||
diff < 3600 && Math.floor( diff / 60 ) + " mins" ||
diff < 7200 && "1 hour" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " d" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " w";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var date = prettyDate(this.title);
if ( date )
jQuery(this).text( date );
});
};
使时间人性化?只需稍加修改,它就可以工作: 在当前插件中,正在使用
this.title
提取日期字符串,它不会为您的
返回任何内容。在您的情况下,我们可以使用$this.text()
提取日期字符串
只要稍加修改,它就可以工作: 在当前插件中,正在使用
this.title
提取日期字符串,它不会为您的
返回任何内容。在您的情况下,我们可以使用$this.text()
提取日期字符串
上面的代码段不考虑时区。如果服务器有UTC时区,则需要额外执行
(d.getTimezoneOffset()*60000)
以转换为本地时间
整个功能如下所示:
function prettyDate(time){
d = new Date();
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = ((d.getTime() + (d.getTimezoneOffset()*60000) - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 min ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " mins ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " week ago";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var $this = jQuery(this),
date = prettyDate($this.text());
if ( date )
$this.text( date );
});
};
$(function() {
$(".p-date").prettyDate();
setInterval(function(){ $(".p-date").prettyDate(); }, 5000);
});
函数prettyDate(时间){
d=新日期();
变量日期=新日期((时间| |“”)。替换(/-/g,“/”)。替换(/[TZ]/g,“”)),
diff=((d.getTime()+(d.getTimezoneOffset()*60000)-date.getTime())/1000),
日差异=数学楼层(差异/86400);
如果(isNaN(日差)| |日差<0 | |日差>=31)
返回;
返回日_diff==0&&(
差值<60&“刚刚”||
差值<120&“1分钟前”||
差异<3600和数学楼层(差异/60)+“分钟前”||
差值<7200&“1小时前”||
差异<86400和数学楼层(差异/3600)+“小时前”)||
day_diff==1&“昨天”||
日差<7日和日差+“日前”||
日差<31&&Math.ceil(日差/7)+“一周前”;
}
//如果jQuery包含在页面中,那么也会添加一个jQuery插件来处理它
if(jQuery的类型!=“未定义”)
jQuery.fn.prettyDate=函数(){
返回此值。每个(函数(){
var$this=jQuery(this),
date=prettyDate($this.text());
若有(日期)
$this.text(日期);
});
};
$(函数(){
$(“.p-date”).prettyDate();
setInterval(函数(){$(“.p-date”).prettyDate();},5000);
});
上述代码片段不考虑时区。如果服务器有UTC时区,则需要额外执行(d.getTimezoneOffset()*60000)
以转换为本地时间
整个功能如下所示:
function prettyDate(time){
d = new Date();
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = ((d.getTime() + (d.getTimezoneOffset()*60000) - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 min ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " mins ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " week ago";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var $this = jQuery(this),
date = prettyDate($this.text());
if ( date )
$this.text( date );
});
};
$(function() {
$(".p-date").prettyDate();
setInterval(function(){ $(".p-date").prettyDate(); }, 5000);
});
函数prettyDate(时间){
d=新日期();
变量日期=新日期((时间| |“”)。替换(/-/g,“/”)。替换(/[TZ]/g,“”)),
diff=((d.getTime()+(d.getTimezoneOffset()*60000)-date.getTime())/1000),
日差异=数学楼层(差异/86400);
如果(isNaN(日差)| |日差<0 | |日差>=31)
返回;
返回日_diff==0&&(
差值<60&“刚刚”||
差值<120&“1分钟前”||
差异<3600和数学楼层(差异/60)+“分钟前”||
差值<7200&“1小时前”||
差异<86400和数学楼层(差异/3600)+“小时前”)||
day_diff==1&“昨天”||
日差<7日和日差+“日前”||
日差<31&&Math.ceil(日差/7)+“一周前”;
}
//如果jQuery包含在页面中,那么也会添加一个jQuery插件来处理它
if(jQuery的类型!=“未定义”)
jQuery.fn.prettyDate=函数(){
返回此值。每个(函数(){
var$this=jQuery(this),
date=prettyDate($this.text());
若有(日期)
$this.text(日期);
});
};
$(函数(){
$(“.p-date”).prettyDate();
setInterval(函数(){$(“.p-date”).prettyDate();},5000);
});
时区的事情怎么样,会处理好吗?我想会的,是的。简单代码会将日期与当前日期时间进行区分,因此只要您提供的日期在日期()对象中,就应该正确地解释它。很好,谢谢,如果有任何其他东西不起作用,我可能会让您出错。不客气。如果可以,我会尽力帮助你,但你可能会通过发帖得到更快(更好)的答案;)时区的事情怎么样,会处理好吗?我想会的,是的。简单代码会将日期与当前日期时间进行区分,因此只要您提供的日期在日期()对象中,就应该正确地解释它。很好,谢谢,如果有任何其他东西不起作用,我可能会让您出错。不客气。如果可以,我会尽力帮助你,但你可能会通过发帖得到更快(更好)的答案;)
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var $this = jQuery(this), // cache jQuery(this)
date = prettyDate($this.text()); // get date string from .text()
if ( date )
$this.text( date );
});
};
function prettyDate(time){
d = new Date();
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = ((d.getTime() + (d.getTimezoneOffset()*60000) - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 min ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " mins ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " week ago";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var $this = jQuery(this),
date = prettyDate($this.text());
if ( date )
$this.text( date );
});
};
$(function() {
$(".p-date").prettyDate();
setInterval(function(){ $(".p-date").prettyDate(); }, 5000);
});