javasctipt如何显示几分钟前、几天前等


Posted in Javascript onApril 30, 2014

jsp页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<base href="<%=basePath%>"> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
<script type="text/javascript" src="<%=basePath%>/js/timeago.js" ></script> 
</head> 
<body> 
This is my JSP page. <br> 
<p>${time}</p> 
<span class="time timeago" title="2014-4-29 15:23"></span> 
</body> 
</html> 
<script type="text/javascript"> 
jQuery("span.timeago").timeago(); 
</script>

(function (factory) { 
if (typeof define === 'function' && define.amd) { 
// AMD. Register as an anonymous module. 
define(['jquery'], factory); 
} else { 
// Browser globals 
factory(jQuery); 
} 
}(function ($) { 
$.timeago = function(timestamp) { 
if (timestamp instanceof Date) { 
return inWords(timestamp); 
} else if (typeof timestamp === "string") { 
return inWords($.timeago.parse(timestamp)); 
} else if (typeof timestamp === "number") { 
return inWords(new Date(timestamp)); 
} else { 
return inWords($.timeago.datetime(timestamp)); 
} 
}; 
var $t = $.timeago; $.extend($.timeago, { 
settings: { 
refreshMillis: 60000, 
allowFuture: false, 
localeTitle: false, 
cutoff: 0, 
strings: { 
prefixAgo: null, 
prefixFromNow: null, 
suffixAgo: "前", 
suffixFromNow: "from now", 
seconds: "1分钟", 
minute: "1分钟", 
minutes: "%d分钟", 
hour: "1小时", 
hours: "%d小时", 
day: "1天", 
days: "%d天", 
month: "1月", 
months: "%d月", 
year: "1年", 
years: "%d年", 
wordSeparator: "", 
numbers: [] 
} 
}, 
inWords: function(distanceMillis) { 
var $l = this.settings.strings; 
var prefix = $l.prefixAgo; 
var suffix = $l.suffixAgo; 
if (this.settings.allowFuture) { 
if (distanceMillis < 0) { 
prefix = $l.prefixFromNow; 
suffix = $l.suffixFromNow; 
} 
} 
var seconds = Math.abs(distanceMillis) / 1000; 
var minutes = seconds / 60; 
var hours = minutes / 60; 
var days = hours / 24; 
var years = days / 365; 
function substitute(stringOrFunction, number) { 
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; 
var value = ($l.numbers && $l.numbers[number]) || number; 
return string.replace(/%d/i, value); 
} 
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || 
seconds < 90 && substitute($l.minute, 1) || 
minutes < 45 && substitute($l.minutes, Math.round(minutes)) || 
minutes < 90 && substitute($l.hour, 1) || 
hours < 24 && substitute($l.hours, Math.round(hours)) || 
hours < 42 && substitute($l.day, 1) || 
days < 30 && substitute($l.days, Math.round(days)) || 
days < 45 && substitute($l.month, 1) || 
days < 365 && substitute($l.months, Math.round(days / 30)) || 
years < 1.5 && substitute($l.year, 1) || 
substitute($l.years, Math.round(years)); 
var separator = $l.wordSeparator || ""; 
if ($l.wordSeparator === undefined) { separator = " "; } 
return $.trim([prefix, words, suffix].join(separator)); 
}, 
parse: function(iso8601) { 
var s = $.trim(iso8601); 
s = s.replace(/\.\d+/,""); // remove milliseconds 
s = s.replace(/-/,"/").replace(/-/,"/"); 
s = s.replace(/T/," ").replace(/Z/," UTC"); 
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 
return new Date(s); 
}, 
datetime: function(elem) { 
var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title"); 
return $t.parse(iso8601); 
}, 
isTime: function(elem) { 
// jQuery's `is()` doesn't play well with HTML5 in IE 
return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time"); 
} 
}); 
// functions that can be called via $(el).timeago('action') 
// init is default when no action is given 
// functions are called with context of a single element 
var functions = { 
init: function(){ 
var refresh_el = $.proxy(refresh, this); 
refresh_el(); 
var $s = $t.settings; 
if ($s.refreshMillis > 0) { 
setInterval(refresh_el, $s.refreshMillis); 
} 
}, 
update: function(time){ 
$(this).data('timeago', { datetime: $t.parse(time) }); 
refresh.apply(this); 
}, 
updateFromDOM: function(){ 
$(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) }); 
refresh.apply(this); 
} 
}; 
$.fn.timeago = function(action, options) { 
var fn = action ? functions[action] : functions.init; 
if(!fn){ 
throw new Error("Unknown function name '"+ action +"' for timeago"); 
} 
// each over objects here and call the requested function 
this.each(function(){ 
fn.call(this, options); 
}); 
return this; 
}; 
function refresh() { 
var data = prepareData(this); 
var $s = $t.settings; 
if (!isNaN(data.datetime)) { 
if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) { 
$(this).text(inWords(data.datetime)); 
} 
} 
return this; 
} 
function prepareData(element) { 
element = $(element); 
if (!element.data("timeago")) { 
element.data("timeago", { datetime: $t.datetime(element) }); 
var text = $.trim(element.text()); 
if ($t.settings.localeTitle) { 
element.attr("title", element.data('timeago').datetime.toLocaleString()); 
} else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) { 
element.attr("title", text); 
} 
} 
return element.data("timeago"); 
} 
function inWords(date) { 
return $t.inWords(distance(date)); 
} 
function distance(date) { 
return (new Date().getTime() - date.getTime()); 
} 
// fix for IE6 suckage 
document.createElement("abbr"); 
document.createElement("time"); 
}));

controller层:
package com.spring.controller; import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Date; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import net.sf.json.JSONArray; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import com.spring.model.JsonMoel; 
import com.sun.org.apache.bcel.internal.generic.NEW; 
/** 
* @author Qixuan.Chen 
* 创建时间:2014-4-29 
*/ 
@Controller 
public class TimeAgoController { 

/** 
* @param request 
* @param response 
* @return 
* @throws IOException 
*/ 
@RequestMapping(value="time/show", method = {RequestMethod.POST,RequestMethod.GET}) 
public ModelAndView PostJsonTest(HttpServletRequest request,HttpServletResponse response) throws IOException{ 
ModelAndView mav=new ModelAndView(); 
mav.addObject("time", new Date()); 
mav.setViewName("time/timeago"); 
return mav; 
} 
}
Javascript 相关文章推荐
Prototype的Class.create函数解析
Sep 22 Javascript
页面加载完成后再执行JS的jquery写法以及区别说明
Feb 22 Javascript
javascript轻量级模板引擎juicer使用指南
Jun 22 Javascript
jquery实现简单的表单验证
Nov 17 Javascript
jQuery实现响应鼠标事件的图片透明效果【附demo源码下载】
Jun 16 Javascript
JavaScript学习笔记整理_关于表达式和语句
Sep 19 Javascript
BootStrap中关于Select下拉框选择触发事件及扩展
Nov 22 Javascript
Angular.js中$resource高大上的数据交互详解
Jul 30 Javascript
Django使用多数据库的方法
Sep 06 Javascript
js实现点击按钮复制文本功能
Jul 20 Javascript
jQuery实现基本隐藏与显示效果的方法详解
Sep 05 jQuery
使用angularjs.foreach时return的问题解决
Sep 30 Javascript
jquery实现更改表格行顺序示例
Apr 30 #Javascript
使用原生js写的一个简单slider
Apr 29 #Javascript
js判断横竖屏及禁止浏览器滑动条示例
Apr 29 #Javascript
使用jquery写个更改表格行顺序的小功能
Apr 29 #Javascript
百度移动版的url编码解码示例
Apr 29 #Javascript
通过url查找a元素应用案例
Apr 29 #Javascript
jquery实现的图片点击滚动效果
Apr 29 #Javascript
You might like
php解析字符串里所有URL地址的方法
2015/04/03 PHP
PHP的伪随机数与真随机数详解
2015/05/27 PHP
ThinkPHP3.2框架操作Redis的方法分析
2019/05/05 PHP
远离JS灾难css灾难之 js私有函数和css选择器作为容器
2011/12/11 Javascript
JS命名空间的另一种实现
2013/08/09 Javascript
使用js简单实现了tree树菜单
2013/11/20 Javascript
使用js实现关闭js弹出层的窗口
2014/02/10 Javascript
JavaScript实现汉字转换为拼音的库文件示例
2016/12/22 Javascript
Bootstrap Table使用整理(三)
2017/06/09 Javascript
js保留两位小数方法总结
2018/01/31 Javascript
Node.js成为Web应用开发最佳选择的原因
2018/02/05 Javascript
vue项目中vue-i18n和element-ui国际化开发实现过程
2018/04/25 Javascript
Vue混入mixins滚动触底的方法
2019/11/22 Javascript
vue props 一次传多个值实例
2020/07/22 Javascript
[05:53]敌法师的金色冠名ID"BurNIng",是传说,是荣耀
2020/07/11 DOTA
跟老齐学Python之有容乃大的list(4)
2014/09/28 Python
用Python写脚本,实现完全备份和增量备份的示例
2018/04/29 Python
解决python读取几千万行的大表内存问题
2018/06/26 Python
Flask之flask-script模块使用
2018/07/26 Python
对python当中不在本路径的py文件的引用详解
2018/12/15 Python
python 实现得到当前时间偏移day天后的日期方法
2018/12/31 Python
pygame实现非图片按钮效果
2019/10/29 Python
pymysql的简单封装代码实例
2020/01/08 Python
使用 Python 读取电子表格中的数据实例详解
2020/04/17 Python
10个python爬虫入门实例(小结)
2020/11/01 Python
python的数学算法函数及公式用法
2020/11/18 Python
CSS3属性box-shadow使用指南
2014/12/09 HTML / CSS
服装机修工岗位职责
2013/12/26 职场文书
2014年清明节寄语
2014/04/03 职场文书
抗洪救灾先进集体事迹材料
2014/05/26 职场文书
乡镇务虚会发言材料
2014/10/20 职场文书
2014保险公司个人工作总结
2014/12/09 职场文书
文艺晚会开场白
2015/05/29 职场文书
《中华上下五千年》读后感3篇
2019/11/29 职场文书
Python利器openpyxl之操作excel表格
2021/04/17 Python
解决MySQL Varchar 类型尾部空格的问题
2022/04/06 MySQL