#I need help with converting daytime to human readable 12 hour format
1 messages · Page 1 of 1 (latest)
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
const t = new Date();
console.log(t.toTimeString())
console.log(t.toISOString())
console.log(t.toString())
Program Output
16:20:00 GMT+0000 (Coordinated Universal Time)
Remember M9#8416 | javascript | nodejs-16.14.0 | wandbox.org
There's a few more formats
Program Output
16:21:35 GMT+0000 (Coordinated Universal Time)
2023-05-10T16:21:35.247Z
Wed May 10 2023 16:21:35 GMT+0000 (Coordinated Universal Time)
Remember M9#8416 | javascript | nodejs-16.14.0 | wandbox.org