Install the SdFat library - there is an example called Timestamp.ino describing in detail how to do it..
// after your rtc is set up and working you code just needs:
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());
}
//put that outside of any functions, before your 'setup'
//Then inside your 'loop' or 'setup'
void loop(void)
{
//Other code that does stuff
//put this next line *Right Before* any file open line:
SdFile::dateTimeCallback(dateTime);
dataFile = SD.open(datafilename, FILE_WRITE);
//other code that does stuff
}