# Add DATE and TIME to your SD CARD Files.

**URL:** <https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901>\
**Category:** Storage\
**Created:** [June 18, 2015, 9:05pm UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901 "2015-06-18T21:05:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 18, 2015, 9:05pm UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901/1 "2015-06-18T21:05:29Z")

</div>

I needed to add the normal Date/Time stamp to the SD CARD files my project creates, to ensure my final product will impress customers as being professional, rather than seeming "hobbyist". But the forum topics I found for the purpose require more libraries which in turn require initialization statements; all of which consumes more of the Uno's limited memory. In addition, comments on those topics revealed those libraries only work for some hardware/software versions, leaving more questions than answers.

So.... using some code provided by [Nick Gammon](http://forum.arduino.cc/index.php?topic=263519.0) and some by [fat16lib](http://forum.arduino.cc/index.php?topic=72739.0), I created a sketch that only needs the SD.h library already running your SD Card. I hand-entered a "fake" time/date, so you can easily test the sketch. But to make it useful, you'll need to add code to retrieve the real time/date from whatever hardware source you are using. In my project, I'm using [this realtime clock](http://www.miniinthebox.com/ds1302-real-time-clock-module-for-arduino-works-with-official-arduino-boards-2-0-5-5v_p1141513.html).

And here's my test sketch:

```plaintext
#include <SD.h>

File file;
const uint8_t CS = 10; // SD chip select

    // YOUR SKETCH SHOULD UPDATE THESE SIX VALUSE
    // EACH TIME BEFORE CREATING YOUR SD CARD FILE
unsigned int year = 2015;
byte month = 6;      
byte day = 18;
byte hour = 7;
byte minute = 8;
byte second = 9;

void dateTime(uint16_t* date, uint16_t* time)
{
  *date = FAT_DATE(year, month, day);
  *time = FAT_TIME(hour, minute, second);
}

void setup()
{
  Serial.begin(9600);
  
      // THIS LINE SETS YOUR SKETCH TO SAVE YOUR
      // TIME AND DATE INTO ANY FILE YOU CREATE.
  SdFile::dateTimeCallback(dateTime);
  
  if (!SD.begin(CS))
  {
    Serial.println("SD.begin failed");
    while(1);
  }
  file = SD.open("TEST_IT.TXT", FILE_WRITE);
  file.close();
  Serial.println("Done");
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![gjt211](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gjt211](https://forum.arduino.cc/u/gjt211)\
**Post date:** [October 20, 2015, 6:16am UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901/2 "2015-10-20T06:16:00Z")

</div>

Thanks, that worked perfectly for me using the standard Arduino SD library.

---

<div class="post-metadata">

**Author:** ![fatfenders](https://avatars.discourse-cdn.com/v4/letter/f/9de053/32.png) [@fatfenders](https://forum.arduino.cc/u/fatfenders)\
**Post date:** [March 22, 2019, 11:20pm UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901/3 "2019-03-22T23:20:41Z")

</div>

Best and simplest example of this code I have viewed! Works great.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 13, 2020, 8:03am UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901/4 "2020-11-13T08:03:21Z")

</div>

Thread opened for comment as requested

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 4:26pm UTC](https://forum.arduino.cc/t/add-date-and-time-to-your-sd-card-files/318901/5 "2021-05-06T16:26:45Z")

</div>


