User:Gary/comments in local time.js: Difference between revisions

Content deleted Content added
add more docs
eslint cleanup
Line 1:
/* eslint-disable complexity, max-statements */
/**
* COMMENTS IN LOCAL TIME
Line 30 ⟶ 29:
function convertMonthToNumber(month) {
return new Date(`${month} 1, 2001`).getMonth();
}
 
function getDates(time) {
const [, oldHour, oldMinute, oldDay, oldMonth, oldYear] = time;
 
// Today
const today = new Date();
 
// Yesterday
const yesterday = new Date();
 
yesterday.setDate(yesterday.getDate() - 1);
 
// Tomorrow
const tomorrow = new Date();
 
tomorrow.setDate(tomorrow.getDate() + 1);
 
// Set the date entered.
const newTime = new Date();
 
newTime.setUTCFullYear(oldYear, convertMonthToNumber(oldMonth), oldDay);
newTime.setUTCHours(oldHour);
newTime.setUTCMinutes(oldMinute);
 
return { time: newTime, today, tomorrow, yesterday };
}
 
Line 86 ⟶ 111:
 
adjustTime(originalTimestamp, search) {
const { time, today, tomorrow, yesterday } = getDates(
let time = originalTimestamp.match(search);
originalTimestamp.match(search)
const [, oldHour, oldMinute, oldDay, oldMonth, oldYear] = time;
);
 
// Today
const today = new Date();
 
// Yesterday
const yesterday = new Date();
 
yesterday.setDate(yesterday.getDate() - 1);
 
// Tomorrow
const tomorrow = new Date();
 
tomorrow.setDate(tomorrow.getDate() + 1);
 
// Set the date entered.
time = new Date();
time.setUTCFullYear(oldYear, convertMonthToNumber(oldMonth), oldDay);
time.setUTCHours(oldHour);
time.setUTCMinutes(oldMinute);
 
// A string matching the date pattern was found, but it cannot be
Line 114 ⟶ 121:
}
 
const date = this.determineDateText({
// Determine the time offset.
time,
const utcValue = (-1 * time.getTimezoneOffset()) / 60;
const utcOffset =today,
tomorrow,
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
yesterday,
});
 
//const Set{ theampm, datehour bits} to= outputthis.getHour(time);
const year = time.getFullYear();
const month = addLeadingZero(time.getMonth() + 1);
const day = time.getDate();
let hour = parseInt(time.getHours(), 10);
const minute = addLeadingZero(time.getMinutes());
 
// Output am or pm depending on the date.
let ampm = '';
 
if (this.LocalComments.twentyFourHours) {
hour = addLeadingZero(hour);
} else {
ampm = hour <= 11 ? ' am' : ' pm';
 
if (hour > 12) {
hour -= 12;
} else if (hour === 0) {
hour = 12;
}
}
 
// return 'today' or 'yesterday' if that is the case
let date;
 
if (
year === today.getFullYear() &&
month === addLeadingZero(today.getMonth() + 1) &&
day === today.getDate()
) {
date = this.language.Today;
} else if (
year === yesterday.getFullYear() &&
month === addLeadingZero(yesterday.getMonth() + 1) &&
day === yesterday.getDate()
) {
date = this.language.Yesterday;
} else if (
year === tomorrow.getFullYear() &&
month === addLeadingZero(tomorrow.getMonth() + 1) &&
day === tomorrow.getDate()
) {
date = this.language.Tomorrow;
} else {
// calculate day of week
const dayNames = [
this.language.Sunday,
this.language.Monday,
this.language.Tuesday,
this.language.Wednesday,
this.language.Thursday,
this.language.Friday,
this.language.Saturday,
];
const dayOfTheWeek = dayNames[time.getDay()];
let descriptiveDifference = '';
let last = '';
 
// Create a relative descriptive difference
if (this.LocalComments.dateDifference) {
({ descriptiveDifference, last } = this.createRelativeDate(
today,
time
));
}
 
const monthName = this.convertNumberToMonth(time.getMonth());
 
// format the date according to user preferences
let formattedDate = '';
 
switch (this.LocalComments.dateFormat.toLowerCase()) {
case 'dmy':
formattedDate = `${day} ${monthName} ${year}`;
 
break;
case 'mdy':
formattedDate = `${monthName} ${day}, ${year}`;
 
break;
default:
formattedDate = `${year}-${month}-${addLeadingZero(day)}`;
}
 
let formattedDayOfTheWeek = '';
 
if (this.LocalComments.dayOfWeek) {
formattedDayOfTheWeek = `, ${last}${dayOfTheWeek}`;
}
 
date = formattedDate + formattedDayOfTheWeek + descriptiveDifference;
}
 
const finalTime = `${hour}:${minute}${ampm}`;
let returnDate;
 
// Determine the time offset.
const utcValue = (-1 * time.getTimezoneOffset()) / 60;
const utcOffset =
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
 
if (this.LocalComments.timeFirst) {
Line 221 ⟶ 144:
}
 
return [{ returnDate, time] };
}
 
Line 239 ⟶ 162:
this.language.December,
][number];
}
 
createDateText({ day, month, time, today, year }) {
// calculate day of week
const dayNames = [
this.language.Sunday,
this.language.Monday,
this.language.Tuesday,
this.language.Wednesday,
this.language.Thursday,
this.language.Friday,
this.language.Saturday,
];
const dayOfTheWeek = dayNames[time.getDay()];
let descriptiveDifference = '';
let last = '';
 
// Create a relative descriptive difference
if (this.LocalComments.dateDifference) {
({ descriptiveDifference, last } = this.createRelativeDate(
today,
time
));
}
 
const monthName = this.convertNumberToMonth(time.getMonth());
 
// format the date according to user preferences
let formattedDate = '';
 
switch (this.LocalComments.dateFormat.toLowerCase()) {
case 'dmy':
formattedDate = `${day} ${monthName} ${year}`;
 
break;
case 'mdy':
formattedDate = `${monthName} ${day}, ${year}`;
 
break;
default:
formattedDate = `${year}-${month}-${addLeadingZero(day)}`;
}
 
let formattedDayOfTheWeek = '';
 
if (this.LocalComments.dayOfWeek) {
formattedDayOfTheWeek = `, ${last}${dayOfTheWeek}`;
}
 
return formattedDate + formattedDayOfTheWeek + descriptiveDifference;
}
 
Line 248 ⟶ 221:
* @returns {Object.<string, *>} Relative date data
*/
// eslint-disable-next-line max-statements
createRelativeDate(today, time) {
/**
Line 263 ⟶ 237:
*/
let daysAgo = Math.abs(Math.round(millisecondsAgo / 1000 / 60 / 60 / 24));
letconst { differenceWord, last } = '';this.relativeText({
let last = '';daysAgo,
millisecondsAgo,
});
 
// TheThis datemethod isof incomputing the pastyears and months is not exact. However,
// it's better than the previous method that used 1 January + delta days.
if (millisecondsAgo >= 0) {
// That was usually quite off because it mapped the second delta month to
differenceWord = this.language.ago;
 
if (daysAgo <= 7) {
last = `${this.language.last} `;
}
 
// The date is in the future.
} else {
differenceWord = this.language['from now'];
 
if (daysAgo <= 7) {
last = `${this.language.this} `;
}
}
 
// This method of computing the years & months is not exact. However, it's
// better than the previous method that used 1 January + delta days. That
// was usually quite off because it mapped the second delta month to
// February, which has only 28 days. This method is usually not more than
// one day off, except perhaps over very distant dates.
 
/**
Line 331 ⟶ 290:
const descriptiveParts = [];
 
// There is years text to add.
if (yearsAgo > 0) {
descriptiveParts.push(
const fmtYears = `${yearsAgo} ${pluralize(
this.language.year,`${yearsAgo} ${pluralize(
yearsAgo this.language.year,
this.language.years yearsAgo,
)}`; this.language.years
)}`
 
descriptiveParts.push(fmtYears);
}
 
// There is months text to add.
if (monthsAgo > 0) {
descriptiveParts.push(
const fmtMonths = `${monthsAgo} ${pluralize(
this.language.month,`${monthsAgo} ${pluralize(
monthsAgo this.language.month,
this.language.months monthsAgo,
)}`; this.language.months
)}`
 
descriptiveParts.push(fmtMonths);
}
 
// There is days text to add.
if (daysAgo > 0) {
descriptiveParts.push(
const fmtDays = `${daysAgo} ${pluralize(
this.language.day,`${daysAgo} ${pluralize(
daysAgo this.language.day,
this.language.days daysAgo,
)}`; this.language.days
)}`
 
descriptiveParts.push(fmtDays);
}
 
Line 369 ⟶ 331:
}
 
determineDateText({ time, today, tomorrow, yesterday }) {
// Set the date bits to output.
const year = time.getFullYear();
const month = addLeadingZero(time.getMonth() + 1);
const day = time.getDate();
 
// return 'today' or 'yesterday' if that is the case
if (
year === today.getFullYear() &&
month === addLeadingZero(today.getMonth() + 1) &&
day === today.getDate()
) {
return this.language.Today;
}
 
if (
year === yesterday.getFullYear() &&
month === addLeadingZero(yesterday.getMonth() + 1) &&
day === yesterday.getDate()
) {
return this.language.Yesterday;
}
 
if (
year === tomorrow.getFullYear() &&
month === addLeadingZero(tomorrow.getMonth() + 1) &&
day === tomorrow.getDate()
) {
return this.language.Tomorrow;
}
 
return this.createDateText({ day, month, time, today, year });
}
 
getHour(time) {
let ampm;
let hour = parseInt(time.getHours(), 10);
 
if (this.LocalComments.twentyFourHours) {
ampm = '';
hour = addLeadingZero(hour);
} else {
// Output am or pm depending on the date.
ampm = hour <= 11 ? ' am' : ' pm';
 
if (hour > 12) {
hour -= 12;
} else if (hour === 0) {
hour = 12;
}
}
 
return { ampm, hour };
}
 
relativeText({ daysAgo, millisecondsAgo }) {
let differenceWord = '';
let last = '';
 
// The date is in the past.
if (millisecondsAgo >= 0) {
differenceWord = this.language.ago;
 
if (daysAgo <= 7) {
last = `${this.language.last} `;
}
 
// The date is in the future.
} else {
differenceWord = this.language['from now'];
 
if (daysAgo <= 7) {
last = `${this.language.this} `;
}
}
 
return { differenceWord, last };
}
 
// eslint-disable-next-line max-statements
replaceText(node, search) {
if (!node) {
Line 398 ⟶ 440:
const beforeMatch = value.substring(0, position);
const afterMatch = value.substring(position + stringLength);
const timeArray{ returnDate, time } = this.adjustTime(match.toString(), search);
const timestamp = timeArray[1] ? timeArray[1]match.getTimetoString() : '';,
search
);
const timestamp = time ? time.getTime() : '';
 
// Is the "timestamp" attribute used for microformats?
Line 409 ⟶ 454:
span.setAttribute('timestamp', timestamp);
span.title = match;
span.append(document.createTextNode(timeArray[0]returnDate));
 
parent = node.parentNode;