dustojnikhummer

timedate2

Jan 29th, 2025 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. -- Wrap the monitor on the right side
  2. local monitor = peripheral.wrap("right")
  3.  
  4. -- Function to convert in-game days to a calendar date
  5. local function convertToDate(day)
  6. local year = math.floor((day - 1) / 365) + 1
  7. local dayOfYear = (day - 1) % 365 + 1
  8.  
  9. local months = {
  10. {name = "January", days = 31},
  11. {name = "February", days = 28},
  12. {name = "March", days = 31},
  13. {name = "April", days = 30},
  14. {name = "May", days = 31},
  15. {name = "June", days = 30},
  16. {name = "July", days = 31},
  17. {name = "August", days = 31},
  18. {name = "September", days = 30},
  19. {name = "October", days = 31},
  20. {name = "November", days = 30},
  21. {name = "December", days = 31},
  22. }
  23.  
  24. local month = 1
  25. local dayOfMonth = dayOfYear
  26.  
  27. for i, m in ipairs(months) do
  28. if dayOfMonth <= m.days then
  29. month = i
  30. break
  31. end
  32. dayOfMonth = dayOfMonth - m.days
  33. end
  34.  
  35. -- Display one additional month
  36. if month == 12 then
  37. month = 1
  38. year = year + 1
  39. else
  40. month = month + 1
  41. end
  42.  
  43. return dayOfMonth, month, year
  44. end
  45.  
  46. -- Function to print the current time and date
  47. local function printTimeAndDate()
  48. -- Clear the monitor
  49. monitor.clear()
  50. monitor.setCursorPos(1, 1)
  51.  
  52. -- Get the current time and date
  53. local time = os.time()
  54. local day = os.day()
  55.  
  56. -- Convert the in-game day to a calendar date
  57. local dayOfMonth, month, year = convertToDate(day)
  58.  
  59. -- Format the time (in-game time from 0 to 24)
  60. local hours = math.floor(time)
  61. local minutes = math.floor((time - hours) * 60)
  62.  
  63. -- Print the date and time on the monitor
  64. monitor.write(string.format("Date: %02d/%02d/%03d", dayOfMonth, month, year))
  65. monitor.setCursorPos(1, 2)
  66. monitor.write(string.format("Time: %02d:%02d", hours, minutes))
  67. end
  68.  
  69. -- Main loop to update the time and date every second
  70. while true do
  71. printTimeAndDate()
  72. sleep(1)
  73. end
  74.  
Advertisement
Add Comment
Please, Sign In to add comment