[SOLVED] DS1307 RTC date and time to convert to string for TFT LCD

Good day!

Please hear me out :-[

I know there's a lot of post regarding converting date and time to string but I assure you Serial.print(now.year(), DEC) is not the answer.

I'm using Arduino MEGA 2560 with a TFT LCD + Touch + SD Card Slot along with its Shield and a TinyRTC Module with DS1307 chip.

I can read and display the date and time. I store the it to SD so I can convert it to string then I print them to the LCD. The problem is I have to constantly save and read values to SD and I think it will affect the time process of my project.

Why don't I print it directly? Text printing in TFT LCD is different. I have to use myGLCD.print(String, x coordinate, y coordinate, deg);

If I want to get the values from the module I have to do this

void saveTime() 
{
   DateTime now;

  // fetch the time
  now = RTC.now();

  // Delete the old One
  SD.remove("time.txt"); 
  // Create new one
  logfile = SD.open("time.txt", FILE_WRITE);

  // writing in the file works just like regular print()/println() function  
  logfile.print("Date/Time=");
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(", ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.println(now.second(), DEC);
  
  // close the file:
  logfile.close();
}

I don't know how to I can store the decimal configuration of now.[date and time] I hope you can help me

Look into sprintf(), it will be very helpful.

Thank you for the reply Sir HazardsMind!

I used this code

void fetchTime()
{
  DateTime now;

  // fetch the time
  now = RTC.now();

  char buffer [25] = "";
  
  sprintf(buffer, "%04d/%02d/%02d, %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
  dateTime = buffer;
  //Serial.print(buffer);
}

It works! Thank you very much :D

Hello arduinoTime, do you still have the code. I have some issues compilated the code. I try to have the Time and date.