Show time/date in viewer's own timezone, Unix epoch conversion

Get help developing custom BBCodes or request one.
defvenom
Registered User
Posts: 2
Joined: Thu Jul 11, 2024 1:44 am

Show time/date in viewer's own timezone, Unix epoch conversion

Post by defvenom »

This is my first post, sorry if it's wrong. I could not find a bbcode like this anywhere.

I'm not sure what to call this, but this bbcode converts a Unix epoch timestamp to human readable format. You can swap around the format if you like, I'm just American.

BBCode:

Code: Select all

[epoch]{TEXT}[/epoch]
HTML replacement:

Code: Select all

<epoch>{TEXT}</epoch>

<script>
document.addEventListener("DOMContentLoaded", function() {
    // Function to convert epoch to human-readable format
    function convertEpochToHumanReadable(epoch) {
        let date = new Date(epoch * 1000);
        let year = date.getFullYear();
        let month = ('0' + (date.getMonth() + 1)).slice(-2);
        let day = ('0' + date.getDate()).slice(-2);
        let hours = ('0' + date.getHours()).slice(-2);
        let minutes = ('0' + date.getMinutes()).slice(-2);
        let seconds = ('0' + date.getSeconds()).slice(-2);
        return `${month}-${day}-${year} ${hours}:${minutes}`;
    }

    // Find all elements with the custom BBCode
    document.querySelectorAll('epoch').forEach(function(element) {
        let epochTimestamp = parseInt(element.innerText);
        if (!isNaN(epochTimestamp)) {
            element.innerText = convertEpochToHumanReadable(epochTimestamp);
        }
    });
});
</script>
Help line:

Code: Select all

replace {TEXT} with a unix timestamp such as one generated from https://www.epochconverter.com/
Time is displayed MM/DD/YYY HH:MM
User avatar
warmweer
Jr. Extension Validator
Posts: 11476
Joined: Fri Jul 04, 2003 6:34 am
Location: Van Allen Bel ... gium

Re: Show time/date in viewer's own timezone, Unix epoch conversion

Post by warmweer »

Unless I'm overlooking something the code you posted converts the Unix time to human time GMT. In other words, it's not taking the timezone into account.

Have a look at Executing PHP Code With BBCodes
Spelling is freeware, which means you can use it for free.
On the other hand, it is not open source, which means you cannot change it or publish it in a modified form.


Time flies like an arrow, but fruit flies like a banana.
defvenom
Registered User
Posts: 2
Joined: Thu Jul 11, 2024 1:44 am

Re: Show time/date in viewer's own timezone, Unix epoch conversion

Post by defvenom »

Oh, when I have people look at a post using the code it shows the date/time in their timezone though. I guess maybe I'm phrasing it wrong but it does what I intended it to do as far as I can tell.

https://pl.petzmainstreet.com/viewtopic.php?p=1789

Return to “Custom BBCode Development and Requests”