Last active
November 24, 2025 14:40
-
-
Save verfasor/d558f68a26cab44a70408fa6e87b07c9 to your computer and use it in GitHub Desktop.
script to show post time next to date on bear blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bear Blog - Show Post Time | |
| // Add this script to your Bear Blog custom JS section (footer directive) to show post time next to date | |
| // Works with single post pages (body.post) | |
| // Explainer: https://mighil.com/script-to-show-post-time-next-to-date-on-bear-blog | |
| (function() { | |
| // Wait for DOM to be ready and Bear's date formatting to complete | |
| function addTimeToPostDates() { | |
| // Only run on single post pages (body has class "post") | |
| if (!document.body.classList.contains('post')) { | |
| return; | |
| } | |
| // Find time elements in the main content area (single post date) | |
| const timeElements = document.querySelectorAll('main time'); | |
| timeElements.forEach(timeEl => { | |
| if (!timeEl.dateTime) return; | |
| const postDate = new Date(timeEl.dateTime); | |
| const now = new Date(); | |
| // Check if the post date is today | |
| const isToday = postDate.getDate() === now.getDate() && | |
| postDate.getMonth() === now.getMonth() && | |
| postDate.getFullYear() === now.getFullYear(); | |
| // Format time in 12-hour format with AM/PM | |
| let hours = postDate.getHours(); | |
| const minutes = postDate.getMinutes(); | |
| const ampm = hours >= 12 ? 'PM' : 'AM'; | |
| hours = hours % 12; | |
| hours = hours ? hours : 12; // 0 should be 12 | |
| const minutesStr = minutes.toString().padStart(2, '0'); | |
| const timeStr = `${hours}:${minutesStr} ${ampm}`; | |
| // Get the current date text (already formatted by Bear) | |
| const currentDateText = timeEl.innerText.trim(); | |
| // Update the text | |
| if (isToday) { | |
| timeEl.innerText = `Today • ${timeStr}`; | |
| } else { | |
| timeEl.innerText = `${currentDateText} • ${timeStr}`; | |
| } | |
| }); | |
| } | |
| // Run when DOM is ready | |
| if (document.readyState === 'loading') { | |
| document.addEventListener('DOMContentLoaded', addTimeToPostDates); | |
| } else { | |
| // DOM is already ready, but wait a bit for Bear's script to run first | |
| setTimeout(addTimeToPostDates, 100); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment