Show friendlier date in PDF reader notes list

Using new Zotero.Date.toFriendlyDate() function

- Time if today
- Day of week if earlier this week
- Date with two-digit year otherwise

We could consider adding 'Yesterday' as well.
This commit is contained in:
Dan Stillman 2021-02-24 18:31:13 -05:00
parent 0c17829b27
commit 3e4ad18b2a
2 changed files with 35 additions and 7 deletions

View file

@ -507,13 +507,7 @@ var ZoteroContextPane = new function () {
var parts = text.split('\n').map(x => x.trim()).filter(x => x.length);
var title = parts[0] && parts[0].slice(0, Zotero.Notes.MAX_TITLE_LENGTH);
var date = Zotero.Date.sqlToDate(note.dateModified);
if (Date.now() - date < 24 * 60 * 60 * 1000) {
date = Zotero.getString('date.today');
date = date.charAt(0).toUpperCase() + date.slice(1);
}
else {
date = Zotero.Date.toRelativeDate(date);
}
date = Zotero.Date.toFriendlyDate(date);
return {
id: note.id,

View file

@ -853,6 +853,40 @@ Zotero.Date = new function(){
}
this.toFriendlyDate = function (date) {
// 6:14:36 PM
if (isToday(date)) {
return date.toLocaleString(false, { hour: 'numeric', minute: 'numeric' })
}
// 'Thursday'
if (isThisWeek(date)) {
return date.toLocaleString(false, { weekday: 'long' });
}
return date.toLocaleDateString(false, { year: '2-digit', month: 'numeric', day: 'numeric' });
};
function isToday(date) {
var d = new Date();
return d.getDate() == date.getDate()
&& d.getMonth() == d.getMonth()
&& d.getFullYear() == d.getFullYear();
}
function isThisWeek(date) {
var d = new Date();
return d.getFullYear() == date.getFullYear() && getWeekNumber(d) == getWeekNumber(date);
}
// https://stackoverflow.com/a/27125580
function getWeekNumber(date) {
let onejan = new Date(date.getFullYear(), 0, 1);
return Math.ceil((((date.getTime() - onejan.getTime()) / 86400000) + onejan.getDay() + 1) / 7);
}
function getFileDateString(file){
var date = new Date();
date.setTime(file.lastModifiedTime);