问题描述
假设我在JavaScript中有两个字符串: var date1 ='2008-10-03T20:24Z'
var date2 ='2008-10-04T12:24Z'
结果如下:
'4周前'
$ p $
$ b或
'在大约15分钟内
(应支持过去和将来)。
有解决方案过去的差异,但我还没有找到支持未来时间差异。
这些是我尝试的解决方案:
和
jQuery解决方案的积分。
解决方案看看你链接的解决方案...它实际上和我无聊的评论一样简单!
这是Zach Leatherman的一个版本代码为您的未来日期添加In。如你所见,这些变化很小。
函数humane_date(date_str){
var time_formats = [
[60,'Just Now'],
[90,'1 Minute'],// 60 * 1.5
[3600,'Minutes',60],// 60 * 60, 60
[5400,'1小时],// 60 * 60 * 1.5
[86400,'小时',3600],// 60 * 60 * 24,60 * 60
[129600,'1天],// 60 * 60 * 24 * 1.5
[604800,'Days',86400],// 60 * 60 * 24 * 7,60 * 60 * 24
[907200,'1 Week'],// 60 * 60 * 24 * 7 * 1.5
[2628000,'Weeks',604800],// 60 * 60 * 24 *(365/12),60 * 60 * 24 * 7
[3942000,'1个月'],// 60 * 60 * 24 *(365/12)* 1.5
[31536000,'Months',2628000],// 60 * 60 * 24 * 365,60 * 60 * 24 *(365/12)
[47304000,'1年'],// 60 * 60 * 24 * 365 * 1.5
[3153600000, '年',31536000],// 60 * 60 * 24 * 365 * 100,60 * 60 * 24 * 365
[4730400000,'1世纪'],// 60 * 60 * 24 * 365 * 100 * 1.5
];
var time =(''+ date_str).replace(/ - / g,/)替换(/ [TZ] / g,),
dt = new日期,
秒=((dt - 新日期(时间)+(dt.getTimezoneOffset()* 60000))/ 1000),
token ='Ago',
prepend ='' ,
i = 0,
格式;
if(seconds< 0){
秒= Math.abs(秒);
token ='';
prepend ='In';
}
while(format = time_formats [i ++]){
if(seconds< format [0]){
if(format.length == 2 ){
return(i> 1?prepend:'')+ format [1] +(i> 1?token:''); //条件所以我们不返回Just Now Ago
} else {
return prepend + Math.round(seconds / format [2])+''+ format [1] +(i> 1?token:'');
}
}
}
//数百年的溢价
如果(秒> 4730400000)
返回Math.round(秒/ 4730400000)+'百年'+令牌;
return date_str;
};
Let's say I've got two strings in JavaScript:
var date1 = '2008-10-03T20:24Z' var date2 = '2008-10-04T12:24Z'
How would I come to a result like so:
'4 weeks ago'
or
'in about 15 minutes'
(should support past and future).
There are solutions out there for the past diffs, but I've yet to find one with support for future time diffs as well.
These are the solutions I tried:
John Resig's Pretty Date and Zach Leatherman's modification
Bonus points for a jQuery solution.
解决方案Looking at the solutions you linked... it is actually as simple as my frivolous comment!
Here's a version of the Zach Leatherman code that prepends "In " for future dates for you. As you can see, the changes are very minor.
function humane_date(date_str){ var time_formats = [ [60, 'Just Now'], [90, '1 Minute'], // 60*1.5 [3600, 'Minutes', 60], // 60*60, 60 [5400, '1 Hour'], // 60*60*1.5 [86400, 'Hours', 3600], // 60*60*24, 60*60 [129600, '1 Day'], // 60*60*24*1.5 [604800, 'Days', 86400], // 60*60*24*7, 60*60*24 [907200, '1 Week'], // 60*60*24*7*1.5 [2628000, 'Weeks', 604800], // 60*60*24*(365/12), 60*60*24*7 [3942000, '1 Month'], // 60*60*24*(365/12)*1.5 [31536000, 'Months', 2628000], // 60*60*24*365, 60*60*24*(365/12) [47304000, '1 Year'], // 60*60*24*365*1.5 [3153600000, 'Years', 31536000], // 60*60*24*365*100, 60*60*24*365 [4730400000, '1 Century'], // 60*60*24*365*100*1.5 ]; var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," "), dt = new Date, seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000), token = ' Ago', prepend = '', i = 0, format; if (seconds < 0) { seconds = Math.abs(seconds); token = ''; prepend = 'In '; } while (format = time_formats[i++]) { if (seconds < format[0]) { if (format.length == 2) { return (i>1?prepend:'') + format[1] + (i > 1 ? token : ''); // Conditional so we don't return Just Now Ago } else { return prepend + Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token : ''); } } } // overflow for centuries if(seconds > 4730400000) return Math.round(seconds / 4730400000) + ' Centuries' + token; return date_str; };
这篇关于如何在JavaScript中呈现两个时间戳之间的上下文差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!