Get text printed at Serial Monitor as String

I’m using this example code (SimpleTime) on ESP32.

#include <WiFi.h>
#include "time.h"

const char* ssid       = "YOUR_SSID";
const char* password   = "YOUR_PASS";

const char* ntpServer = "";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

void printLocalTime()
  struct tm timeinfo;
    Serial.println("Failed to obtain time");
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");

void setup()
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println(" CONNECTED");
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

  //disconnect WiFi as it's no longer needed

void loop()

Serial monitor correctly displays date and time using the format set?
But how can I store this data into a variable (eg String)?

String a = (&timeinfo, "%A, %B %d %Y %H:%M:%S");

This prints nothing at the Serial monitor, whereas this:

String a = &timeinfo;

… returns this error:

conversion from 'tm*' to non-scalar type 'String' requested

How could I store it to a variable (eg String)?

Thank you in advance

I don't immediately know the answer because I am not familiar with the time.h library.

However I suspect you Title may not be attracting people who do know about. I think a more realistic Title would be "How to save ESP32 time value in a String". Your problem has nothing to do with the Serial Monitor. If you edit your Original Post you can change the Title.

From a little research It seems that your timeinfo is a struct that contains all the time components.

Maybe (very wild guess) you just need to sprintf() like this

timeString = sprintf(&timeinfo, "%A, %B %d %Y %H:%M:%S");


Thank you for your reply!!!
It helped me figure it out. In the “little research” link you provided me with, there are the names of the structure variables. So I can call them one by one.

int current_hour = timeinfo.tm_hour;
int current_min = timeinfo.tm_min;

Once again, thank you!