feat: formatted date times one website

This commit is contained in:
Leyla Becker 2026-02-19 16:32:11 -06:00
parent dffd14a19f
commit 927f13b623
5 changed files with 51 additions and 17 deletions

View file

@ -3,5 +3,7 @@ module.exports = {
// SITE_URL=https://blog.jan-leila.com pnpm run build // SITE_URL=https://blog.jan-leila.com pnpm run build
// SITE_URL=https://volpe.jan-leila.com pnpm run build // SITE_URL=https://volpe.jan-leila.com pnpm run build
// SITE_URL=http://your-onion-address.onion pnpm run build // SITE_URL=http://your-onion-address.onion pnpm run build
url: process.env.SITE_URL || "http://localhost:8080" url: process.env.SITE_URL || "http://localhost:8080",
// Timezone for displaying dates (uses IANA timezone names)
timezone: "America/Chicago"
}; };

View file

@ -136,6 +136,28 @@
</script> </script>
{% endif %} {% endif %}
<script>
document.querySelectorAll('.localizable-time').forEach(function(el) {
var iso = el.getAttribute('datetime');
if (!iso) return;
try {
var date = new Date(iso);
if (isNaN(date.getTime())) return;
var options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
};
el.textContent = date.toLocaleString(undefined, options);
} catch (e) {
// Keep server-rendered fallback on error
}
});
</script>
<footer> <footer>
<div> <div>
<!-- Social Things --> <!-- Social Things -->

View file

@ -10,9 +10,9 @@ layout: base.njk
{% endif %} {% endif %}
<header class="post-header"> <header class="post-header">
<h1>{{ title }}</h1> <h1>{{ title }}</h1>
<time datetime="{{ createdAt | dateTimeFormat }}">{{ createdAt | dateTimeFormat }}</time> <time class="localizable-time" datetime="{{ createdAt | isoDateTime }}">{{ createdAt | dateTimeFormat }}</time>
{% if updatedAt and (updatedAt | isMoreThanHourAfter(createdAt)) %} {% if updatedAt and (updatedAt | isMoreThanHourAfter(createdAt)) %}
<p class="updated-at">Updated: <time datetime="{{ updatedAt | dateTimeFormat }}">{{ updatedAt | dateTimeFormat }}</time></p> <p class="updated-at">Updated: <time class="localizable-time" datetime="{{ updatedAt | isoDateTime }}">{{ updatedAt | dateTimeFormat }}</time></p>
{% endif %} {% endif %}
</header> </header>

View file

@ -8,6 +8,7 @@ const fs = require("fs");
const crypto = require("crypto"); const crypto = require("crypto");
const path = require("path"); const path = require("path");
const { DateTime } = require("luxon"); const { DateTime } = require("luxon");
const siteConfig = require("./_data/site.js");
const fileHashCache = {}; const fileHashCache = {};
const getFileHash = (file, dir = "css") => { const getFileHash = (file, dir = "css") => {
@ -193,15 +194,24 @@ module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("dateTimeFormat", (date) => { eleventyConfig.addFilter("dateTimeFormat", (date) => {
const dt = date instanceof Date const dt = date instanceof Date
? DateTime.fromJSDate(date) ? DateTime.fromJSDate(date, { zone: 'utc' })
: DateTime.fromISO(date); : DateTime.fromISO(date, { zone: 'utc' });
return dt.toFormat('MMMM d, yyyy h:mm a'); // Convert to site timezone for display
const displayDt = dt.setZone(siteConfig.timezone);
return displayDt.toFormat('MMMM d, yyyy h:mm a ZZZZ');
});
eleventyConfig.addFilter("isoDateTime", (date) => {
const dt = date instanceof Date
? DateTime.fromJSDate(date, { zone: 'utc' })
: DateTime.fromISO(date, { zone: 'utc' });
return dt.toISO();
}); });
eleventyConfig.addFilter("isoDate", (date) => { eleventyConfig.addFilter("isoDate", (date) => {
const dt = date instanceof Date const dt = date instanceof Date
? DateTime.fromJSDate(date) ? DateTime.fromJSDate(date, { zone: 'utc' })
: DateTime.fromISO(date); : DateTime.fromISO(date, { zone: 'utc' });
return dt.toISODate(); return dt.toISODate();
}); });
@ -209,8 +219,8 @@ module.exports = (eleventyConfig) => {
if (!date1 || !date2) return false; if (!date1 || !date2) return false;
const toDateTime = (d) => { const toDateTime = (d) => {
if (DateTime.isDateTime(d)) return d; if (DateTime.isDateTime(d)) return d;
if (d instanceof Date) return DateTime.fromJSDate(d); if (d instanceof Date) return DateTime.fromJSDate(d, { zone: 'utc' });
return DateTime.fromISO(d); return DateTime.fromISO(d, { zone: 'utc' });
}; };
const dt1 = toDateTime(date1); const dt1 = toDateTime(date1);
const dt2 = toDateTime(date2); const dt2 = toDateTime(date2);

View file

@ -10,7 +10,7 @@ const getGitCreatedTime = (filePath) => {
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] } { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
).trim(); ).trim();
if (result) { if (result) {
return DateTime.fromISO(result); return DateTime.fromISO(result).toUTC();
} }
} catch (e) { } catch (e) {
// Git command failed, return null // Git command failed, return null
@ -25,7 +25,7 @@ const getGitModifiedTime = (filePath) => {
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] } { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
).trim(); ).trim();
if (result) { if (result) {
return DateTime.fromISO(result); return DateTime.fromISO(result).toUTC();
} }
} catch (e) { } catch (e) {
// Git command failed, return null // Git command failed, return null
@ -61,7 +61,7 @@ const getFileCreatedTime = (filePath) => {
try { try {
const stats = fs.statSync(filePath); const stats = fs.statSync(filePath);
const time = stats.birthtime ?? stats.mtime; const time = stats.birthtime ?? stats.mtime;
return DateTime.fromJSDate(time); return DateTime.fromJSDate(time, { zone: 'utc' });
} catch (e) { } catch (e) {
return null; return null;
} }
@ -70,7 +70,7 @@ const getFileCreatedTime = (filePath) => {
const getFileModifiedTime = (filePath) => { const getFileModifiedTime = (filePath) => {
try { try {
const stats = fs.statSync(filePath); const stats = fs.statSync(filePath);
return DateTime.fromJSDate(stats.mtime); return DateTime.fromJSDate(stats.mtime, { zone: 'utc' });
} catch (e) { } catch (e) {
return null; return null;
} }
@ -79,8 +79,8 @@ const getFileModifiedTime = (filePath) => {
const parseDate = (value) => { const parseDate = (value) => {
if (!value) return null; if (!value) return null;
if (DateTime.isDateTime(value)) return value; if (DateTime.isDateTime(value)) return value;
if (value instanceof Date) return DateTime.fromJSDate(value); if (value instanceof Date) return DateTime.fromJSDate(value, { zone: 'utc' });
const parsed = DateTime.fromISO(value); const parsed = DateTime.fromISO(value, { zone: 'utc' });
return parsed.isValid ? parsed : null; return parsed.isValid ? parsed : null;
} }
@ -98,7 +98,7 @@ const getPostCreatedAt = (data) => {
return fileDate; return fileDate;
} }
return DateTime.fromJSDate(data.page.date); return DateTime.fromJSDate(data.page.date, { zone: 'utc' });
} }
module.exports = { module.exports = {