the home site for me: also iteration 3 or 4 of my site

feat: update relative time to use 2 week period

dunkirk.sh 17bd0c11 198814a7

verified
+25 -12
+25 -12
static/js/relative-time.js
··· 32 32 } 33 33 34 34 get threshold() { 35 - return this.getAttribute('threshold') || 'P30D'; 35 + return this.getAttribute('threshold') || 'P14D'; 36 36 } 37 37 38 38 get prefix() { ··· 70 70 this.scheduleUpdate(3600000); 71 71 } else { 72 72 this.textContent = this.formatRelative(diff); 73 - this.scheduleUpdate(this.getNextUpdateDelay(absDiff)); 73 + const delay = this.getNextUpdateDelay(absDiff); 74 + if (delay !== null) this.scheduleUpdate(delay); 74 75 } 75 76 } 76 77 ··· 88 89 return 60000 * 5; 89 90 } else if (days < 7) { 90 91 return 3600000; 92 + } else if (days < 30) { 93 + return 3600000 * 6; 91 94 } else { 92 - return 3600000 * 6; 95 + return null; 93 96 } 94 97 } 95 98 99 + getRtf() { 100 + if (!this._rtf || this._rtfLang !== navigator.language) { 101 + this._rtfLang = navigator.language; 102 + this._rtf = new Intl.RelativeTimeFormat(navigator.language, { 103 + numeric: 'auto', 104 + style: 'long' 105 + }); 106 + } 107 + return this._rtf; 108 + } 109 + 96 110 formatRelative(diff) { 97 - const rtf = new Intl.RelativeTimeFormat(navigator.language, { 98 - numeric: 'auto', 99 - style: 'long' 100 - }); 111 + const rtf = this.getRtf(); 101 112 102 113 const absDiff = Math.abs(diff); 103 114 const sign = diff > 0 ? -1 : 1; ··· 105 116 const minutes = Math.floor(seconds / 60); 106 117 const hours = Math.floor(minutes / 60); 107 118 const days = Math.floor(hours / 24); 108 - const months = Math.floor(days / 30); 109 - const years = Math.floor(days / 365); 119 + const date = new Date(Date.now() - diff); 120 + const now = new Date(); 121 + const months = (now.getFullYear() - date.getFullYear()) * 12 + (now.getMonth() - date.getMonth()); 122 + const years = Math.floor(Math.abs(months) / 12) * sign; 110 123 111 124 if (seconds < 60) { 112 125 return rtf.format(sign * seconds, 'second'); ··· 116 129 return rtf.format(sign * hours, 'hour'); 117 130 } else if (days < 30) { 118 131 return rtf.format(sign * days, 'day'); 119 - } else if (months < 12) { 120 - return rtf.format(sign * months, 'month'); 132 + } else if (Math.abs(months) < 12) { 133 + return rtf.format(months, 'month'); 121 134 } else { 122 - return rtf.format(sign * years, 'year'); 135 + return rtf.format(years, 'year'); 123 136 } 124 137 } 125 138