格式YYYY-MM-DDTHH:MM:SS + 0000的js中的相对时间函数 [英] Relative time function in js for format YYYY-MM-DDTHH:MM:SS+0000
本文介绍了格式YYYY-MM-DDTHH:MM:SS + 0000的js中的相对时间函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
进入相对时间作为30秒前
我已经有一个功能,它的类似但不同的时间格式:
function relative_time(time_value,is_relative){
var values = time_value.split(),
parsed_date = Date.parse(values [1] + + values [2] +,+ values [5] ++ values [3] +UTC),
date = new Date(parsed_date),
relative_to = new Date ),
r ='',
delta = parseInt((relative_to.getTime() - date.getTime())/ 1000);
var seconds = {
'from':{
'minutes':function(v){return v * 60; },
'hours':function(v){return this.minutes(v)* 60; },
'days':function(v){return this.hours(v)* 24; },
'weeks':function(v){return this.days(v)* 7; },
'months':function(v){return this.weeks(v)* 4.34812141; },
'years':function(v){return this.months(v)* 12; }
},
'to':{
'minutes':function(v){return v / 60; },
'hours':function(v){return this.minutes(v)/ 60; },
'days':function(v){return this.hours(v)/ 24; },
'weeks':function(v){return this.days(v)/ 7; },
'months':function(v){return this.weeks(v)/ 4.34812141; },
'years':function(v){return this.months(v)/ 12; }
}
};
if(!is_relative)
return formatTime(date)+''+ formatDate(date);
if(delta< 30)
返回'少于一分钟前';
var minutes = parseInt(seconds.to.minutes(delta)+0.5);
if(minutes< = 1)
大约一分钟前返回';
var hours = parseInt(seconds.to.hours(delta)+0.5);
if(hours< 1)
return minutes +'minutes ago';
if(hours == 1)
返回大约一个小时前;
var days = parseInt(seconds.to.days(delta)+0.5);
if(days< 1)
return hours +'hours ago';
if(days == 1)
return formatTime(date)+'yesterday';
var weeks = parseInt(seconds.to.weeks(delta)+0.5);
if(weeks< 2)
return formatTime(date)+''+ days +'days ago';
var months = parseInt(seconds.to.months(delta)+0.5);
if(months< 2)
return weeks +'weeks ago';
var years = parseInt(seconds.to.years(delta)+0.5);
if(years< 2)
return months +'months ago';
返回年+'年前';
函数formatTime(date){
var hour = date.getHours(),
min = date.getMinutes()+,
ampm ='AM ';
if(hour> = 12)ampm ='PM';
if(hour> 12)hour - = 12;
if(min.length == 1){
min ='0'+ min;
}
return hour +':'+ min +''+ ampm;
}
};
此功能的格式为:Fri Nov 06 02:53:43 +0000
如何更改此脚本以处理新的时间格式?
谢谢
解决方案
我不知道是否要使用jQuery,但是:
I was wondering if anyone knew how I could parse this type of date format: 2010-07-26T18:02:46+0000
into the relative time such as "30 seconds ago"
I already have a function which does it for a similar but different time format:
function relative_time(time_value, is_relative) {
var values = time_value.split(" "),
parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3] + " UTC"),
date = new Date(parsed_date),
relative_to = new Date(),
r = '',
delta = parseInt((relative_to.getTime() - date.getTime()) / 1000);
var seconds = {
'from' : {
'minutes' : function(v) { return v * 60; },
'hours' : function(v) { return this.minutes(v) * 60; },
'days' : function(v) { return this.hours(v) * 24; },
'weeks' : function(v) { return this.days(v) * 7; },
'months' : function(v) { return this.weeks(v) * 4.34812141; },
'years' : function(v) { return this.months(v) * 12; }
},
'to' : {
'minutes' : function(v) { return v / 60; },
'hours' : function(v) { return this.minutes(v) / 60; },
'days' : function(v) { return this.hours(v) / 24; },
'weeks' : function(v) { return this.days(v) / 7; },
'months' : function(v) { return this.weeks(v) / 4.34812141; },
'years' : function(v) { return this.months(v) / 12; }
}
};
if (!is_relative)
return formatTime(date) + ' ' + formatDate(date);
if (delta < 30)
return 'less than a minute ago';
var minutes = parseInt(seconds.to.minutes(delta)+0.5);
if (minutes <= 1)
return 'about a minute ago';
var hours = parseInt(seconds.to.hours(delta)+0.5);
if (hours < 1)
return minutes + ' minutes ago';
if (hours == 1)
return 'about an hour ago';
var days = parseInt(seconds.to.days(delta)+0.5);
if (days < 1)
return hours + ' hours ago';
if (days==1)
return formatTime(date) + ' yesterday';
var weeks = parseInt(seconds.to.weeks(delta)+0.5);
if (weeks < 2)
return formatTime(date) + ' ' + days + ' days ago';
var months = parseInt(seconds.to.months(delta)+0.5);
if (months < 2)
return weeks + ' weeks ago';
var years = parseInt(seconds.to.years(delta)+0.5);
if (years < 2)
return months + ' months ago';
return years + ' years ago';
function formatTime(date) {
var hour = date.getHours(),
min = date.getMinutes() + "",
ampm = 'AM';
if (hour >= 12) ampm = 'PM';
if (hour > 12) hour -= 12;
if (min.length == 1) {
min = '0' + min;
}
return hour + ':' + min + ' ' + ampm;
}
};
Format for this function would be: Fri Nov 06 02:53:43 +0000
How could I change this script to work with the new time format? It's a bit beyond me at this stage and I am keen to learn.
Thank You
解决方案
I don't know if you want to use jQuery but:
There is a jQuery plugin for it.
这篇关于格式YYYY-MM-DDTHH:MM:SS + 0000的js中的相对时间函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文