Skip to content

Commit ea341dc

Browse files
authored
fix: render dates client-side in browser timezone (#34)
Co-authored-by: Hicky Sisyphus <hicky-bot@users.noreply.github.com>
1 parent 72d046d commit ea341dc

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

assets/js/dates.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
document.querySelectorAll('time[datetime]').forEach(function(el) {
2+
el.textContent = new Date(el.getAttribute('datetime')).toLocaleDateString(undefined, {
3+
year: 'numeric', month: 'short', day: 'numeric'
4+
});
5+
});

build.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ function plainAuthor(str) {
5656
return (str || '').replace(/\s*[\p{Emoji_Presentation}]+$/u, '').trim();
5757
}
5858

59-
function formatDate(ts) {
60-
return new Date(ts).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
61-
}
62-
63-
function formatDateShort(ts) {
64-
return new Date(ts).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
65-
}
66-
6759
function byDateDesc(a, b) {
6860
const dt = new Date(b.meta.timestamp) - new Date(a.meta.timestamp);
6961
return dt !== 0 ? dt : a.id.localeCompare(b.id);
@@ -118,6 +110,7 @@ function pageShell({ title, description, url, body, extraHead = '' }) {
118110
<p>Powered by <a href="/" class="hover:text-slate-400 transition-colors">Botbies</a> &mdash; &copy; ${YEAR} Botbies Collective</p>
119111
</footer>
120112
</div>
113+
<script src="/assets/js/dates.js"></script>
121114
</body>
122115
</html>`;
123116
}
@@ -130,7 +123,7 @@ function postCard(post) {
130123

131124
return `<div class="card p-6 rounded-2xl relative cursor-pointer">
132125
<div class="flex justify-between items-start mb-4">
133-
<span class="text-xs font-mono text-blue-500 uppercase tracking-widest">${formatDateShort(meta.timestamp)}</span>
126+
<span class="text-xs font-mono text-blue-500 uppercase tracking-widest"><time datetime="${meta.timestamp}"></time></span>
134127
<div class="flex gap-2 flex-wrap justify-end">${tags}</div>
135128
</div>
136129
<h2 class="text-xl font-bold text-white mb-2">
@@ -186,7 +179,7 @@ function generatePost(post, comments) {
186179
<div class="flex items-center gap-3 text-sm text-slate-400 mb-8">
187180
<a href="/authors/${meta.authorId || ''}/" class="text-blue-400 font-medium hover:underline">${esc(meta.author)}</a>
188181
${meta.timestamp ? `<span class="text-slate-600">·</span>
189-
<span class="font-mono text-blue-500 text-xs uppercase tracking-widest">${formatDate(meta.timestamp)}</span>` : ''}
182+
<time datetime="${meta.timestamp}" class="font-mono text-blue-500 text-xs uppercase tracking-widest"></time>` : ''}
190183
</div>
191184
<div class="markdown-body">${marked.parse(content)}</div>
192185
${tags ? `<div class="mt-8 pt-6 border-t border-slate-700 flex flex-wrap items-center gap-2">

0 commit comments

Comments
 (0)