user settings: change 'Date uploaded' display format

Display 'Today' or 'Yesterday' and the date for days beyond those
This commit is contained in:
Utkarsh Patil
2017-12-05 03:05:59 +04:00
committed by showell
parent 0b710e41c9
commit 25f742839d
2 changed files with 27 additions and 4 deletions

View File

@@ -31,7 +31,7 @@ exports.set_up_attachments = function () {
var attachments = page_params.attachments;
_.each(attachments, function (attachment) {
attachment.create_time_str = timerender.absolute_time(attachment.create_time);
attachment.create_time_str = timerender.relative_date(attachment.create_time);
attachment.size_str = exports.bytes_to_size(attachment.size);
});

View File

@@ -8,6 +8,9 @@ var set_to_start_of_day = function (time) {
return time.setMilliseconds(0).setSeconds(0).setMinutes(0).setHours(0);
};
var MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// Given an XDate object 'time', returns an object:
// {
// time_str: a string for the current human-formatted version
@@ -214,9 +217,6 @@ exports.get_full_time = function (timestamp) {
// this is for rendering absolute time based off the preferences for twenty-four
// hour time in the format of "%mmm %d, %h:%m %p".
exports.absolute_time = (function () {
var MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var fmt_time = function (date, H_24) {
var payload = {
hours: date.getHours(),
@@ -254,6 +254,29 @@ exports.absolute_time = (function () {
};
}());
// this is for rendering date relative to current time
// and should display "Today" or "Yesterday" if timestamp date is
// today's or yesterday's, if not display the format "%mmm %d".
exports.relative_date = function (timestamp, today) {
if (typeof today === 'undefined') {
today = new Date();
}
var date = new Date(timestamp);
var date_diff = today.getDate() - date.getDate();
if (date_diff === 0) {
return "Today";
} else if (date_diff === 1) {
return "Yesterday";
}
var is_older_year = (today.getFullYear() - date.getFullYear()) > 0;
var str = MONTHS[date.getMonth()] + " " + date.getDate();
// include year if message date is from a previous year
if (is_older_year) {
str += ", " + date.getFullYear();
}
return str;
};
// XDate.toLocaleDateString and XDate.toLocaleTimeString are
// expensive, so we delay running the following code until we need
// the full date and time strings.