зеркало из https://github.com/mozilla/snowl.git
properly format dates in the future
This commit is contained in:
Родитель
2c05696c5a
Коммит
20b18ad15a
|
@ -72,48 +72,26 @@ let SnowlUtils = {
|
||||||
* @returns a human-readable string representing the date
|
* @returns a human-readable string representing the date
|
||||||
*/
|
*/
|
||||||
_formatDate: function(date) {
|
_formatDate: function(date) {
|
||||||
let result;
|
let day = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
||||||
|
|
||||||
let now = new Date();
|
let now = new Date();
|
||||||
|
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
||||||
|
|
||||||
let today = new Date(now.getFullYear(), now.getMonth, now.getDate());
|
let yesterday = new Date(now - 1000 * 60 * 60 * 24);
|
||||||
|
|
||||||
let yesterday = new Date(now - 24 * 60 * 60 * 1000);
|
|
||||||
yesterday = new Date(yesterday.getFullYear(),
|
yesterday = new Date(yesterday.getFullYear(),
|
||||||
yesterday.getMonth(),
|
yesterday.getMonth(),
|
||||||
yesterday.getDate());
|
yesterday.getDate());
|
||||||
|
|
||||||
let sixDaysAgo = new Date(now - 6 * 24 * 60 * 60 * 1000);
|
let sixDaysAgo = new Date(now - 1000 * 60 * 60 * 24 * 6);
|
||||||
sixDaysAgo = new Date(sixDaysAgo.getFullYear(),
|
sixDaysAgo = new Date(sixDaysAgo.getFullYear(),
|
||||||
sixDaysAgo.getMonth(),
|
sixDaysAgo.getMonth(),
|
||||||
sixDaysAgo.getDate());
|
sixDaysAgo.getDate());
|
||||||
|
|
||||||
if (date.toLocaleDateString() == now.toLocaleDateString())
|
// If it's in the future or more than six days in the past, format it
|
||||||
result = this._dfSvc.FormatTime("",
|
// as a full date/time string, i.e.: 2008-05-13 15:37:42.
|
||||||
this._dfSvc.timeFormatNoSeconds,
|
if (day > today || day < sixDaysAgo)
|
||||||
date.getHours(),
|
return this._dfSvc.FormatDateTime("",
|
||||||
date.getMinutes(),
|
Ci.nsIScriptableDateFormat.dateFormatShort,
|
||||||
null);
|
Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
|
||||||
else if (date > yesterday)
|
|
||||||
result = "Yesterday " + this._dfSvc.FormatTime("",
|
|
||||||
this._dfSvc.timeFormatNoSeconds,
|
|
||||||
date.getHours(),
|
|
||||||
date.getMinutes(),
|
|
||||||
null);
|
|
||||||
else if (date > sixDaysAgo)
|
|
||||||
result = this._dfSvc.FormatDateTime("",
|
|
||||||
this._dfSvc.dateFormatWeekday,
|
|
||||||
this._dfSvc.timeFormatNoSeconds,
|
|
||||||
date.getFullYear(),
|
|
||||||
date.getMonth() + 1,
|
|
||||||
date.getDate(),
|
|
||||||
date.getHours(),
|
|
||||||
date.getMinutes(),
|
|
||||||
date.getSeconds());
|
|
||||||
else
|
|
||||||
result = this._dfSvc.FormatDateTime("",
|
|
||||||
this._dfSvc.dateFormatShort,
|
|
||||||
this._dfSvc.timeFormatNoSeconds,
|
|
||||||
date.getFullYear(),
|
date.getFullYear(),
|
||||||
date.getMonth() + 1,
|
date.getMonth() + 1,
|
||||||
date.getDate(),
|
date.getDate(),
|
||||||
|
@ -121,7 +99,34 @@ let SnowlUtils = {
|
||||||
date.getMinutes(),
|
date.getMinutes(),
|
||||||
date.getSeconds());
|
date.getSeconds());
|
||||||
|
|
||||||
return result;
|
// If it's today, only show the time.
|
||||||
|
if (day.getTime() == today.getTime())
|
||||||
|
return this._dfSvc.FormatTime("",
|
||||||
|
Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
|
||||||
|
date.getHours(),
|
||||||
|
date.getMinutes(),
|
||||||
|
null);
|
||||||
|
|
||||||
|
// If it's yesterday, show "Yesterday" plus the time.
|
||||||
|
// FIXME: make this localizable.
|
||||||
|
if (day.getTime() == yesterday.getTime())
|
||||||
|
return "Yesterday " +
|
||||||
|
this._dfSvc.FormatTime("",
|
||||||
|
Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
|
||||||
|
date.getHours(),
|
||||||
|
date.getMinutes(),
|
||||||
|
null);
|
||||||
|
|
||||||
|
// It's two to six days ago, so show the day of the week plus the time.
|
||||||
|
return this._dfSvc.FormatDateTime("",
|
||||||
|
Ci.nsIScriptableDateFormat.dateFormatWeekday,
|
||||||
|
Ci.nsIScriptableDateFormat.timeFormatNoSeconds,
|
||||||
|
date.getFullYear(),
|
||||||
|
date.getMonth() + 1,
|
||||||
|
date.getDate(),
|
||||||
|
date.getHours(),
|
||||||
|
date.getMinutes(),
|
||||||
|
date.getSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Загрузка…
Ссылка в новой задаче