#I need help with converting daytime to human readable 12 hour format

1 messages · Page 1 of 1 (latest)

fickle sentinel
#

Notes:
1.
1 day in Minecraft is 24000 ticks, so 1 hour is 1000 ticks. However, since 1 hour is 1000 ticks, 1 minutes is roughly 16,67 ticks, so it's not perfect every second

0 ticks start at 6.00 AM, so 12.00 AM at night is 18000 ticks

#

Wrap all into system.runInterval

#

add 1 means add 1 ticks

#

Also since 1 hour is 1000 ticks and 1 real-time second is 20 ticks, so Minecraft minutes is capped at 50, not 60

#

16,67 ticks
16 2/3 ticks

#

This is my time function

numberToTime(time) {
    let hour = Math.floor(time / 1000) + 6;
    if (time > 18000) hour -= 18;

    return `${hour <= 10 ? "0" + hour : hour}${
      Math.floor(time / 20) % 2 ? ":" : "."
    }00 ${hour >= 12 ? "PM" : "AM"}`;
  }
#

The ampm is I guess wrong

#

Yes

#

Oh, nvm

#

Yeah it is

#
const totalHours = Math.floor(time / 1000) + 6;
const hours12 = (totalHours) % 12;
const minutes = Math.floor(((time % 1000) / 1000) * 60);
const ampm = totalHours >= 12 ? "PM" : "AM";
const time12 = `${hours12}:${minutes.toString().padStart(2, "0")} ${ampm}`;
#

I moved + 6 to totalHours

#

On your old code, when your hours12 is 3.00 (pm), the ampm still output (am) because you didn't add 6 from totalHours

#
const totalHours = Math.floor(time / 1000) + 6;
const hours12 = (totalHours) % 12;
const ampm = totalHours >= 12 ? "PM" : "AM";

const totalMinutes = time - ((totalHours - 6) * 1000)
const minutes = Math.floor((totalMinutes * 3) / 50)

const time12 = `${hours12.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} ${ampm}`;
#

No, that's because your TIME_UPDATE_INTERVAL

#
import { system, world } from "@minecraft/server";

// Define the function to update the time and scoreboards
function updateTimeAndScoreboards() {
    // Get the current in-game time
    const time = world.getTime();
    console.warn(`Current in-game time: ${world.getTime()}`);

    // Calculate the time in hours and minutes
    const totalHours = Math.floor(time / 1000) + 6;
    const hours12 = (totalHours) % 12;
    const ampm = totalHours >= 12 ? "PM" : "AM";

    const totalMinutes = time - ((totalHours - 6) * 1000)
    const minutes = Math.floor((totalMinutes * 3) / 50)

   const time12 = `${hours12.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} ${ampm}`;

    // Set the gameHour and gameMinute scoreboards
    const setHourCommand = `/scoreboard players set @e[name=timerMike] gameHour ${hours12}`;
    const setMinuteCommand = `/scoreboard players set @e[name=timerMike] gameMinute ${minutes}`;
    world.getDimension('overworld').runCommand(setHourCommand);
    world.getDimension('overworld').runCommand(setMinuteCommand);

    // Display the time in 12-hour format
    console.warn(`Current in-game time: ${time12}`);
}

// Run the updateTimeAndScoreboards function
system.runInterval(updateTimeAndScoreboards);
#

Be careful because of spam

stiff field
#
const t = new Date();
console.log(t.toTimeString())
console.log(t.toISOString())
console.log(t.toString())
quasi garnetBOT
stiff field
#

There's a few more formats

quasi garnetBOT
stiff field
#

Insert the ms time in new Date(<ms time here>)

#

That's what i did in Insect Titans event addon, where the world.time is converted to hh:mm:ss

#

I think it's multiplied to some number