Get text printed at Serial Monitor as String

Hello!
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 = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

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

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

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}

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");
Serial.print(a);

This prints nothing at the Serial monitor, whereas this:

String a = &timeinfo;
Serial.print(a);

… 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");

...R

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.
eg:

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

Once again, thank you!