Can you format __DATE__ ?

Is it possible to change the date format that DATE produces?

At the moment it produces "Dec 22 2013" is it possible to change it to "20131222"?

don't know, it might be dependent on the config of your PC during the compile?

Is it possible to change the date format that DATE produces?

It's up to the compiler to determine the value of that constant. Since it is a compile-time constant, why does the format matter?

I think the date format is defined by the language spec (isn’t it required to be compatible with asctime?) although I don’t know whether/how the months names are localised. I don’t see any easy way to change the format, but I have seen people write macros to parse the standard string to generate different formats such as an ISO 8601 date. The more common solution, but which would be much harder to do in an Arduino environment, is to use make or equivalent to generate a date value in whatever format you require and pass it to the compiler via a command line macro definition.

Quoting from the C99 standard:

DATE
The date of translation of the source file: a character string literal of the
form "Mmm dd yyyy", where the names of the months are the same as
those generated by the asctime function, and the first character of dd is
a space character if the value is less than 10. If the date of translation is not
available, an implementation-defined valid date shall be supplied.

TIME
The time of translation of the source file: a character string literal of the
form "hh:mm:ss" as in the time generated by the asctime function. If
the time of translation is not available, an implementation-defined valid
time shall be supplied.

PaulS:
It’s up to the compiler to determine the value of that constant. Since it is a compile-time constant, why does the format matter?

I use the date on an info page to show the date the sketch was uploaded.
I am using a 20 chr LCD display with limited spare space and wanted a shorter version so I could display something else on the same line.

It shouldn't be too hard to re-arrange. The year and day just have to be moved around, and you look up the month in a table and convert it to a number.

I think this is how I will do it.

I have now spent time online looking in to this and there there seems to be 2 main methods; 1. use a pre-processor script, 2. manipulate the string in the main program.
The pre-processor method is beyond my skill level so I will try to do it in the sketch.

A lot of information on bytes.com and stackoverflow

Well if you use the Arduino IDE, using a preprocessor is out of the question, as the IDE is not that flexible (unless you edit the appropriate files that control the building), and you would have to parse DATE. Fortunately the ISO C/C++ standards mandate the size of DATE and where the numbers go, so it is a matter of dealing with the months.

If you just need to reformat the DATE, that’s pretty easy…

void logname(char const *date, char *buff) { 
    int month, day, year;
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
    sscanf(date, "%s %d %d", buff, &day, &year);
    month = (strstr(month_names, buff)-month_names)/3+1;
    sprintf(buff, "%d%02d%02d.txt", year, month, day);
}

void setup()
{
  Serial.begin(9600); while (!Serial);
  Serial.print("log file name: ");
  char filename[16];
  logname(__DATE__, filename);
  Serial.println(filename);
}

void loop(){
}

Here are the Serial Terminal results…

log file name: 20141009.txt

If you need a time_t or tmElements_t object, it’s a little more involved and I’ve found some limitations. It uses the Time lib referenced here.

#include "Arduino.h"
#include <Time.h>
#include <stdio.h>

time_t cvt_date(char const *date) { 
    char s_month[5];
    int month, day, year;
    tmElements_t tmel;
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

    sscanf(date, "%s %d %d", s_month, &day, &year);

    month = (strstr(month_names, s_month)-month_names)/3+1;

    tmel.Hour = tmel.Minute = tmel.Second = 0; // This was working perfectly until 3am then broke until I added this.
    tmel.Month = month;
    tmel.Day = day;
 // year can be given as full four digit year or two digts (2010 or 10 for 2010);
 //it is converted to years since 1970
  if( year > 99)
      tmel.Year = year - 1970;
  else
      tmel.Year = year + 30;

    return makeTime(tmel);
}

void printdate(char const *date)
{
  Serial.println((String)"cvt_date('" + date + "')");
  time_t t = cvt_date(date);

  Serial.println((String) month(t) + "-" + day(t) + "-" + year(t));
  setTime(t);
  Serial.println((String) month()  + "/" + day()  + "/" + year() + "\n");
}

void setup()
{
  Serial.begin(9600); while (!Serial);
  printdate(__DATE__);       // works with the compiler macro
  printdate("Jan  1    00"); // works with 2 digit years
  printdate("Feb  28   01");
  printdate("Mar  7 5");     // works with 1 digit years
  printdate("Apr  10 1970"); // works from 1970
  printdate("May  13 1980");
  printdate("Jun  16 1990");
  printdate("Jul  19 1997");
  printdate("Aug  22 2000");
  printdate("Sep  25 2010");
  printdate("Oct  31 2014");
  printdate("Nov  30 2020");
  printdate("Dec  31 2105"); // through 2105
  printdate("Dec  31 2106"); // fails at and after 2106
}

void loop(){
}

Here are the Serial Terminal results…

cvt_date('Oct  5 2014')
10-5-2014
10/5/2014

cvt_date('Jan  1    00')
1-1-2000
1/1/2000

cvt_date('Feb  28   01')
2-28-2001
2/28/2001

cvt_date('Mar  7 5')
3-7-2005
3/7/2005

cvt_date('Apr  10 1970')
4-10-1970
4/10/1970

cvt_date('May  13 1980')
5-13-1980
5/13/1980

cvt_date('Jun  16 1990')
6-16-1990
6/16/1990

cvt_date('Jul  19 1997')
7-19-1997
7/19/1997

cvt_date('Aug  22 2000')
8-22-2000
8/22/2000

cvt_date('Sep  25 2010')
9-25-2010
9/25/2010

cvt_date('Oct  31 2014')
10-31-2014
10/31/2014

cvt_date('Nov  30 2020')
11-30-2020
11/30/2020

cvt_date('Dec  31 2105')
12-31-2105
12/31/2105

cvt_date('Dec  31 2106')
11-23-1970
11/23/1970

I’m a beginner so there may be mistakes or bad practices in here. Hope this helps.