24Hour instead of AM/PM

Hi forum

I am all new to this, and need a little help. I have this code, but i want it to show 24 hours instead of AM/PM. Is it possible?

  // AM/PM
  bool isPM = false;
  if (hours >= 12) {
    isPM = true;
    if (hours > 12) hours -= 12;
  }
  if (hours == 0) hours = 12;

Regards Klaus

That snippet of code that you have shown us is doing a conversion from 24 hour format to AM/PM.

The variable 'hours' is in 24 hour at the start, but if it is greater than 12 then 12 is subtracted.
Just don't use that code and it won't be converted.

If you showed us the complete code, then a better reply could be given.

You might want to look at this How to get the best out of this forum before you proceed any further.

It tells you about how to post code correctly in these here parts.

I have some doubts that the OP willingly put each line in code tags. I suspect that this is still caused by the forum software.

The way the forum acts, from day to day, changes. One moment it is behaving correctly and the next it changes. It is in a hissy fit mode today, but yesterday it was fine.

Hello JohnLincoln

The complete code is found at: DIY Stellar Clock

And copied in below here. I think it’s a funny little item to fiddle with now the summer has gone.

#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_SHT31.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// ===== WiFi info =====
const char* ssid     = "Your_Wifi";
const char* password = "Pass";

// ===== OLED setup =====
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// ===== SHT31 setup =====
Adafruit_SHT31 sht31 = Adafruit_SHT31();

// ===== NTP setup =====

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "``pool.ntp.org``", 7 * 3600, 60000); // GMT+7

// ===== Pins  =====
#define I2C_SDA 8
#define I2C_SCL 9
#define LED_PIN 0
// LED
unsigned long lastBlink = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin(I2C_SDA, I2C_SCL);
  // WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected!");

  // OLED init
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  display.setRotation(1);
  display.display();

  // SHT31 init
  if (!sht31.begin(0x44)) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  // NTP init
  timeClient.begin();

  // LED init
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}
void loop() {
  timeClient.update();

  int hours = timeClient.getHours();
  int minutes = timeClient.getMinutes();
  int seconds = timeClient.getSeconds();

  // AM/PM
  bool isPM = false;
  if (hours >= 12) {
    isPM = true;
    if (hours > 12) hours -= 12;
  }
  if (hours == 0) hours = 12;
  // Đọc cảm biến
  float tempC = sht31.readTemperature();
  float hum   = sht31.readHumidity();

  // Format chuỗi
  char hStr[3], mStr[3], sStr[3];
  sprintf(hStr, "%02d", hours);
  sprintf(mStr, "%02d", minutes);
  sprintf(sStr, "%02d", seconds);

  // ===== OLED =====
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  // Header
  display.setTextSize(0.5);
  display.setCursor(2, 0);
  display.println("/////");
  display.setCursor(60, 10);
  display.println(isPM ? "PM" : "AM");
  display.drawLine(0, 10, 128, 10, SSD1306_WHITE);

  // hh
  display.setTextSize(2);
  display.setCursor(5, 30);
  display.println(hStr);

  // mm
  display.setTextSize(2);
  display.setCursor(5, 52);
  display.println(mStr);

  // ss
  display.setTextSize(2);
  display.setCursor(5, 72);
  display.println(sStr);

  // --------
  display.drawLine(0, 95, 128, 95, SSD1306_WHITE);

  // (°C) & Độ ẩm
  display.setTextSize(1);
  display.setCursor(5, 105);
  display.print((int)tempC);
  display.print((char)247); // ký hiệu °
  display.print("C");
  display.setCursor(5, 120);
  display.print((int)hum);
  display.print("%");

  display.display();
  // ===== LED =====
  unsigned long now = millis();
  if (now - lastBlink >= 5000) {  // mỗi 5 giây
    digitalWrite(LED_PIN, HIGH);
    delay(20);                    // sáng 20 ms
    digitalWrite(LED_PIN, LOW);
    lastBlink = now;
  }

  delay(200); // giảm tải CPU
}

hello birkla

Try this:

#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_SHT31.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// ===== WiFi info =====
const char* ssid = "Your_Wifi";
const char* password = "Pass";

// ===== OLED setup =====
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// ===== SHT31 setup =====
Adafruit_SHT31 sht31 = Adafruit_SHT31();

// ===== NTP setup =====
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7 * 3600, 60000);  // GMT+7

// ===== Pins  =====
#define I2C_SDA 8
#define I2C_SCL 9
#define LED_PIN 0

// LED
unsigned long lastBlink = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin(I2C_SDA, I2C_SCL);

  // WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected!");

  // OLED init
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }
  display.clearDisplay();
  display.setRotation(1);
  display.display();

  // SHT31 init
  if (!sht31.begin(0x44)) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }

  // NTP init
  timeClient.begin();

  // LED init
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  timeClient.update();

  int hours = timeClient.getHours();
  int minutes = timeClient.getMinutes();
  int seconds = timeClient.getSeconds();


  // AM/PM
  // not needed for 24 hour clock
  /*
  bool isPM = false;
  if (hours >= 12) {
    isPM = true;
    if (hours > 12) hours -= 12;
  }
  if (hours == 0) hours = 12;
  */


  // Đọc cảm biến
  float tempC = sht31.readTemperature();
  float hum = sht31.readHumidity();

  // Format chuỗi
  char hStr[3], mStr[3], sStr[3];
  sprintf(hStr, "%02d", hours);
  sprintf(mStr, "%02d", minutes);
  sprintf(sStr, "%02d", seconds);

  // ===== OLED =====
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  // Header
  display.setTextSize(0.5);
  display.setCursor(2, 0);
  display.println("/////");

  // not needed for 24 hour clock
  /*
  display.setCursor(60, 10);
  display.println(isPM ? "PM" : "AM");
  */

  display.drawLine(0, 10, 128, 10, SSD1306_WHITE);

  // hh
  display.setTextSize(2);
  display.setCursor(5, 30);
  display.println(hStr);

  // mm
  display.setTextSize(2);
  display.setCursor(5, 52);
  display.println(mStr);

  // ss
  display.setTextSize(2);
  display.setCursor(5, 72);
  display.println(sStr);

  // --------
  display.drawLine(0, 95, 128, 95, SSD1306_WHITE);

  // (°C) & Độ ẩm
  display.setTextSize(1);
  display.setCursor(5, 105);
  display.print((int)tempC);
  display.print((char)247);  // ký hiệu °
  display.print("C");
  display.setCursor(5, 120);
  display.print((int)hum);
  display.print("%");

  display.display();

  // ===== LED =====
  unsigned long now = millis();
  if (now - lastBlink >= 5000) {  // mỗi 5 giây
    digitalWrite(LED_PIN, HIGH);
    delay(20);  // sáng 20 ms
    digitalWrite(LED_PIN, LOW);
    lastBlink = now;
  }

  delay(200);  // giảm tải CPU
}

The part that you showed in the first post just needed removing, together with the 2 lines that were used to display the AM/PM indicator.
I've 'commented them out' to do the removing, so you can see the changes.

I don't have the correct hardware to test the code,
So I tested using a Nano ESP32, but I used a modified version of the code to use the Serial Monitor instead of the display.
I used dummy data for the temperature and humidity.

Here are the Serial Monitor outputs for the AM/PM version and the 24 hour version: