Timeago.js可以很好地格式化相对于当前时间的时间戳(“约2小时前”)。
是否有任何解决方案可以完成同样的事情,但缩写? IE“2h”,就像Twitter一样。
答案 0 :(得分:4)
您可以使用Timeago.js并使用缩写版本覆盖本地时间格式,例如
var locale = function(number, index, total_sec) {
return [
['just now', 'right now'],
['%s s ago', 'in %s s'],
['1 m ago', 'in 1 m'],
['%s m ago', 'in %s m'],
['1 h ago', 'in 1 h'],
['%s h ago', 'in %s h'],
....
][index];
};
timeago.register('pt_ABBR', locale);
var timeagoInstance = timeago();
// then you can use it
timeagoInstance.format(1473245023718, 'pt_ABBR');
答案 1 :(得分:0)
在timeago.js中,你可以简单地用“strings”hash替换“strings”哈希:https://github.com/rmm5t/jquery-timeago/blob/master/locales/jquery.timeago.en-short.js
jQuery.timeago.settings.strings = {
prefixAgo: null,
prefixFromNow: null,
suffixAgo: "",
suffixFromNow: "",
seconds: "1m",
minute: "1m",
minutes: "%dm",
hour: "1h",
hours: "%dh",
day: "1d",
days: "%dd",
month: "1mo",
months: "%dmo",
year: "1yr",
years: "%dyr",
wordSeparator: " ",
numbers: []
};