SD card file date.


Is there anyway of changing the file date when a new file is created? (assuming the current date is known!)

Currently it seems all files are created with a date of 1st Jan 2000.

My current work-around is to include the current date in the filename, but this a far from ideal solution.


I am doing that in a temperature logger. This is with the SdFat library.

Assuming you know the time (as in the variables below):

unsigned int year;
byte month, day, hour, minute, second;

//This function returns the date and time for SD card file access and modify time. 
void fileDateTime (uint16_t* date, uint16_t* time) 
  *date=FAT_DATE(year, month, day);
  *time=FAT_TIME(hour, minute, second);
  }  // end of fileDateTime

void setup () 
  // Attach callback function to provide date time to files. 
  SdFile::dateTimeCallback (fileDateTime); 

Now when the SdFat library wants to know the date and time it calls fileDateTime. At this point those variables should have the correct time in them (read your clock chip at that point if you have to).

Thanks Nick,

This is with the SdFat library.

I used to use the SdFat library (along with Sd2Card.h, FatStructs.h, Sd2PinMap.h, SdFatmainpage.h, SdFatUtil.h, SdInfo.h) but I 'upgraded' to using the SD.h library (which I understand is based on SdFat)

Can you offer any advice on how to use the code you've provided? Do I need to go back to using the files listed above? Do I need to hack (insert/modify) any base libraries?

Sorry, to press for details but anything with pointers in it immediately scrambles my brain!


I don't know which version of the Arduino IDE you have, and thus which version of the SD library you have.

I'm using version 1.0

(Before you ask why I haven't upgraded, it's because more recent versions aren't compatible with the amount of progmem I use on some of my large projects - it's something to do with whereabouts in memory progmem items get loaded. To cut a long story short, if I upgrade my projects stop working!)