Date in .txt file

I'm making a data logger and stocking information in a SD card, I have a RTC module to know when I got the examples but there's an issue: I can't get the correct date when I save the .txt file.

Inside the file, the date & time are correct, but when I check the saving date from the file, it appears to be saved at 1:00:00 01/01/2000
Any suggestions?

If you are using the SD class, that is expected behavior. How is the SD class supposed to know the date and time?

The SdFat library supports assigning dates and times to files.

Thank you for your help.

You could use the date as the filename. I find it convenient to start a new file at midnight, and there is no need to record the date on the file. No need to use SDFat either.

Nick_Pyner Thanks for the advice but I need to get the date right.

PaulS Once I add the SdFat library to Arduino and run the examples there's an error message: "sketch_may04a.ino:6:21: fatal error: FatFile.h: No such file or directory" And I've already tried four times.

guerrero-1106:
Nick_Pyner Thanks for the advice but I need to get the date right.

I guess I better not ask why. What I described is common practice.

Once I add the SdFat library

From?

and run the examples

If they won't compile, you can hardly run them.

guerrero-1106:
Once I add the SdFat library to Arduino and run the examples there's an error message: "sketch_may04a.ino:6:21: fatal error: FatFile.h: No such file or directory" And I've already tried four times.

Strange...
Where do you downloaded the library?
Looks like something in the library's folder structure is not right...

If you don’t wish to use SDFat, you can get timestamps for the file modification dates using SD.h and the dateTimeCallback function and the FAT_DATE and FAT_TIME macros.

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

File file;  // test file
const uint8_t SD_CS = 10; // SD chip select
RTC_DS1307 RTC;  // define the Real Time Clock object

//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
 DateTime now = RTC.now();
 // return date using FAT_DATE macro to format fields
 *date = FAT_DATE(now.year(), now.month(), now.day());
 // return time using FAT_TIME macro to format fields
 *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
void setup() {
 
 Serial.begin(9600);
 Wire.begin();
 if (!RTC.begin()) {
   Serial.println("RTC failed");
   while(1);
 }

 // set date time callback function
 SdFile::dateTimeCallback(dateTime);
 
 if (!SD.begin(SD_CS)) {
   Serial.println("SD.begin failed");
   while(1);
 }
 file = SD.open("TEST4_SD.TXT", FILE_WRITE);
 file.println("Testing 1,2,3...");
 file.close();
 Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

Have you set up the RTC to the correct time?

If you are using RTClib.h this uses

RTC.adjust(DateTime(__DATE__, __TIME__));

from the system time on your PC.

Then SD.h

File logfile; // the logging file

........................................

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.print(now.second(), DEC);
  logfile.println(", ");

to write time data to your SD card.

G

Thanks all for your help, but I can't get to work neither the RTClib.h nor the SDFat.h and I don´t know why. By the moment I'm using DS1307RTC.h and TimeLib.h

PaulS How do I hardly run them?

Cattledog thanks for the code but as I said, the RTClib.h is not working for me.

PaulS How do I hardly run them?

It is not rocket science. Compare the ratio of questions we have asked to answers you have supplied. Notice that the ratio is greater than one. When it gets down to 1.0000, your code will have a much better chance of working.

Re-read ALL of the replies, and make sure that you have supplied an answer, or do so now.

but as I said, the RTClib.h is not working for me.

Inside the file, the date & time are correct,

?? ?? ??

If the correct date and time did not come from the RTC, where did they come from?

I thought the issue was with the "saving date from the file" when looking at the SD files on a computer.

These are two very different problems. Which one are you having?