Try the following sketch:
#include "RTClib.h" //for DS3231 RTC contains Wire.h
byte prSec = 0;
RTC_DS3231 rtc; //user data type RTC_DS3231 variable rtc or object
DateTime nowDT;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
Serial.begin(9600);
rtc.begin();
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//takes time of the day from PC
rtc.adjust(DateTime(2021, 12, 31, 01, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}
void loop ()
{
showTime();
timeDelay();//1-sec interval of time display; it comes from DS3231 itself
}
//===================================================
void showTime()
{
nowDT = rtc.now(); //nowDT hold Date and Time
//---------------------------
byte myHour = nowDT.hour(); //myHour holds Hour of time of daya
if (myHour < 10)
{
Serial.print('0');
}
Serial.print(myHour); Serial.print(':'); //12:58:57 12:4:56
//-------------------------------------------------
byte myMin = nowDT.minute(); //to show leading zero of minute
if (myMin < 10)
{
Serial.print('0');
}
Serial.print(nowDT.minute()); Serial.print(':');
//--------------------------------------------------
byte mySec = nowDT.second();
if (mySec < 10)
{
Serial.print('0');
}
Serial.print(mySec);//(nowDT.second(), DEC);
//----------------------------------------- `
Serial.print(" ");
Serial.print(daysOfTheWeek[nowDT.dayOfTheWeek()]); Serial.print(' ');
byte myDay = nowDT.day();
if (myDay < 10)
{
Serial.print('0');
}
Serial.print(myDay); Serial.print(':');
//-----------------------------------------------------
byte myMonth = nowDT.month();
if (myMonth < 10)
{
Serial.print('0');
}
Serial.print(nowDT. month()); Serial.print(':');
Serial.println(nowDT.year());
}
//---------------------------------------------
void timeDelay()
{
prSec = bcdSecond(); //current second of RTC
while (bcdSecond() == prSec) // != 1 )
{
;
}
prSec = bcdSecond(); //delay(1000);
}
byte bcdSecond()
{
nowDT = rtc.now();
if (nowDT.second() == 0 )
{
return 0;
}
else
{
return nowDT.second();
}
}