Save NTP time to RTC in ESP32

I noticed that when ESP32 is turned on, the time is displayed. About the internal RTC clock that the ESP32 has? Is it possible to continuously update this time using NTP? If so, does anyone have a code please? Next how to list RTC time.
Thank you very much for your help

This is one of the rare super-standard-examples I have a ready to use code for.
As long as the WLAN-connection is active the time is synchronised through the WLAN-connection

#include <WiFi.h>

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;

//const char *ssid     = "WLANBuero_EXT"; 
const   char *ssid     = "FRITZ!Box 7490";

const char *password = "";

const char* ntpServer = "";
const long  gmtOffset_sec = 0;
const int   daylightOffset_sec = 7200;

#include <time.h>                   // time() ctime()
time_t now;                         // this is the epoch
tm myTimeInfo;                      // the structure tm holds time information in a more convient way

void showTime() {
  time(&now);                       // read the current time
  localtime_r(&now, &myTimeInfo);           // update the structure tm with the current time
  Serial.print(myTimeInfo.tm_year + 1900);  // years since 1900
  Serial.print(myTimeInfo.tm_mon + 1);      // January = 0 (!)
  Serial.print(myTimeInfo.tm_mday);         // day of month
  Serial.print(myTimeInfo.tm_hour);         // hours since midnight  0-23
  Serial.print(myTimeInfo.tm_min);          // minutes after the hour  0-59
  Serial.print(myTimeInfo.tm_sec);          // seconds after the minute  0-61*
  Serial.print(myTimeInfo.tm_wday);         // days since Sunday 0-6
  if (myTimeInfo.tm_isdst == 1)             // Daylight Saving Time flag

void connectToWifi() {
  Serial.print("Connecting to "); 


  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    BlinkHeartBeatLED(OnBoard_LED, 333);
  Serial.print("\n connected.");
  Serial.println(WiFi.localIP() );


void synchroniseWith_NTP_Time() {
  Serial.print("configTime uses ntpServer ");
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  Serial.print("synchronising time");
  while (myTimeInfo.tm_year + 1900 < 2000 ) {
    time(&now);                       // read the current time
    localtime_r(&now, &myTimeInfo);
    BlinkHeartBeatLED(OnBoard_LED, 100);
  Serial.print("\n time synchronsized \n");

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.print( F("  compiled ") );
  Serial.print( F(" ") );

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  else return false;            // not expired

void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 

void setup() {
  Serial.println("\n Setup-Start \n");

void loop() {

  if ( TimePeriodIsOver(MyTestTimer,1000) ) {

best regards Stefan

there is a NTP example in the IDE for the ESP32!

Nevertheless if you want to follow a smaller sketch, you might read my page
it includes DST and explaines what happens.

thanks for the link werner - nice page; I'll try it now!

1 Like

How do I use a date into a string in the form HH: MM DD.MM.YYYY
Is it possible to list UNIX time? I.e. time in seconds since 1970?

The 'now' variable of datatype 'time_t' returned by the 'time(&now)' function is NTP Epoch Time (unsigned long seconds since Jan 1, 1900 I believe). It can be converted to Unix time.

How do I use a date into a string in the form HH: MM DD.MM.YYYY

Thank you very much for your help

localtime_r(&now, &myTimeInfo);

'myTimeInfo' is of type 'tm'. This is a struct. You can see its definition in time.h:

struct tm
  int	tm_sec;
  int	tm_min;
  int	tm_hour;
  int	tm_mday;
  int	tm_mon;
  int	tm_year;
  int	tm_wday;
  int	tm_yday;
  int	tm_isdst;
#ifdef __TM_GMTOFF
  long	__TM_GMTOFF;
#ifdef __TM_ZONE
  const char *__TM_ZONE;

Once you have the local time in a variable of type 'tm' you can use its members to format any type of string you want.

Actually, looking more closely at the source code, I see that the ESP32 version of <time.h> defines 'time_t' as a 'long'. So, it can't be referenced to NPT time epoch ( Jan 1, 1900). But, it is an epoch time and it shouldn't be too difficult for you to figure out the offset required to convert it to Unix time.

snprintf is your friend in building ANY data into a string.

I really do not know. I try it for several hours and I can't write the required time in the form HH: MM DD.MM.YYYY

I don't even know how to list UNIX time.

Will you please advise again? Very, very please.

long unsigned int epochTime;
struct tm timeinfo;
char buf[40];
strftime(buf, sizeof buf, "%H:%M %d-%m-%Y", &tm);
epochTime = buf;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.