Content deleted Content added
Copied from User:Mxn/CommentsInLocalTime.js (1271846134) |
Migrate to Intl.RelativeTimeFormat and Intl.DateTimeFormat |
||
Line 10:
* @author [[User:Mxn]]
*/
const DateFormatter = require("mediawiki.DateFormatter");
/**
Line 229 ⟶ 231:
/**
* Returns a formatted string for the given
*
* @param {
* @param {String} fmt A format string or function.
* @returns {String} A formatted string.
*/
function
return fmt(then);
} else {▼
}▼
}
Line 247 ⟶ 253:
function formatTimestamp(idx, elt) {
var iso = $(elt).attr("datetime");
var then =
var now =
<= moment.relativeTimeThreshold("h");▼
var formats = LocalComments.formats;
var lang = mw.config.get("wgPageViewLanguage");
var text;▼
▲ }
▲ else {
▲ }
}▼
$(elt).text(text);▼
// Add a tooltip with multiple formats.
elt.title = $.map(LocalComments.tooltipFormats, function (fmt, idx) {
return
}).join("\n");
// Register for periodic updates.
var value;
var unit;
var seconds = (now - then) / 1000; // convert ms to s
<= moment.relativeTimeThreshold("m");▼
value = seconds;
var minutes = seconds / 60;
unit = "minutes";
▲ }
var hours = minutes / 60;
value = hours;
unit = "hours";
}
var days = hours / 24;
value = days;
unit = "days";
}
var weeks = days / 7;
if (Math.abs(days) > 7) {
value = weeks;
unit = "weeks";
}
$(elt).attr("data-localcomments-unit", unit);
// Replace the text.
var format;
▲ var text;
if (unit === "weeks") {
format = new Intl.DateTimeFormat(lang, {dateStyle: "long", timeStyle: "short"});
text = format.format(then);
} else {
format = new Intl.RelativeTimeFormat(lang, {numeric: "auto"});
text = format.format(Math.round(value), unit);
}
▲ $(elt).text(text);
}
|