-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJquery.jTimestampToLocal.js
executable file
·58 lines (58 loc) · 2.26 KB
/
Jquery.jTimestampToLocal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* jTimestampToLocal
* @description 将指定位置的时间戳转换本地时间并替换到标签内
* @date 2013.1.7
* @version 1.0.1
* @author Khiyuan.Fan <[email protected]>
* @license http://creativecommons.org/licenses/by-sa/3.0/cn/ 本作品采用知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可
*
****************************************************************************
* @since 1.0.1 添加部分简单的格式化输出内容,暂时有 %Y %m %d %H %M %S
* @since 1.0
****************************************************************************
* jTimestampToLocal
* @param json
* {
* pos : '', 时间戳保存位置,'inner'为标签内,其它则为标签的对应属性
* format: '' 格式化输出,默认'%Y/%m/%d %H:%M:%S'
* }
* @return string
****************************************************************************
* @todo 丰富格式化输出的内容
**/
(function($) {
$.fn.jTimestampToLocal=function(options){
var opts = $.extend({}, $.fn.jTimestampToLocal.defaults, options);
return this.each(function() {
$this = $(this);
var pos = opts.pos;
switch(pos)
{
case 'inner':
var timestamp = $this.html();
break
default :
var timestamp = $(this).attr(pos);
break
}
var str_date = $.fn.jTimestampToLocal.GetLocal(timestamp);
$this.html('<time>'+str_date+'<time>');
});
}
$.fn.jTimestampToLocal.defaults={
pos : 'inner',
format : '%Y/%m/%d %H:%M:%S'
};
$.fn.jTimestampToLocal.GetLocal=function(timestamp){
alert(opts.format);
var date1 = new Date(parseInt(timestamp)*1000);
var str_year = date1.getFullYear();
var str_Month = (date1.getMonth()+1).toString();
var str_Day = date1.getDate();
var str_Hour = date1.getHours();
var str_Minute = date1.getMinutes();
var str_second = date1.getSeconds();
var str_date = date1.getFullYear()+"/"+(date1.getMonth()+1)+"/"+date1.getDate()+" "+date1.getHours()+":"+date1.getMinutes()+":"+date1.getSeconds();
return(str_date);
}
})(jQuery);