# SD Card Library, file date & time fields question

**URL:** <https://forum.arduino.cc/t/sd-card-library-file-date-time-fields-question/892816>\
**Category:** Storage\
**Created:** [August 5, 2021, 7:42pm UTC](https://forum.arduino.cc/t/sd-card-library-file-date-time-fields-question/892816 "2021-08-05T19:42:07Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [August 5, 2021, 11:42pm UTC](https://forum.arduino.cc/t/sd-card-library-file-date-time-fields-question/892816/4 "2021-08-05T23:42:55Z")

</div>

Here's a link to a previous thread on this subject. You want a "date time callback function".  
[https://forum.arduino.cc/t/file-creation-date-and-time-in-sd-card/336037/4](https://forum.arduino.cc/t/file-creation-date-and-time-in-sd-card/336037/4)

Here's an example using SD.h

```cpp
//http://forum.arduino.cc/index.php?topic=348562.msg3298426;topicseen#msg3298426

#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

 char timestamp[30];

//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
 DateTime now = RTC.now();
 sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(),now.minute(),now.second(),now.month(),now.day(),now.year()-2000);
 Serial.println("yy");
 Serial.println(timestamp);
 // 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);
 
  DateTime now = RTC.now();
  sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(),now.minute(),now.second(),now.month(),now.day(),now.year()-2000);
  Serial.println("xx");
  Serial.println(timestamp);
 
 
 if (!SD.begin(SD_CS)) {
   Serial.println("SD.begin failed");
   while(1);
 }
 file = SD.open("TEST_SD.TXT", FILE_WRITE);
 file.println("Testing 1,2,3...");
 
 delay(5000);
 file.close();
 Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}
```

---

_[View the full topic](https://forum.arduino.cc/t/sd-card-library-file-date-time-fields-question/892816)._
