Arduino date And TIme

how to get the time and date using arduino in arduino version 1.8.1

We'd need to know more about what you're trying to do and what hardware you're using to do it. If you want the time and date when the sketch was compiled you can use the DATE and TIME macros:

void setup() {
  Serial.begin(9600);
  while(!Serial){}
  Serial.println("Compilation date:");
  Serial.println(__DATE__);
  Serial.println("Compilation time: ");
  Serial.println(__TIME__);
}

void loop() {}

But that's only going to tell you when the sketch was uploaded to the board, not the current time/date after that.

You might also say something about what you are doing and why you need date and time.

The Arduino keeps track of time since it was last reset through it's internal counter (and an external crystal, in most cases). You can poll how many milliseconds have past through the function millis(), but it overflows and resets after around 54 days.

If you want actual dates and times that relates to the good old birth of Christ, the usual way is to hook up an RTC (Real Time Clock) module to your Arduino. They are not very expensive and there's a bunch of libraries around to handle them. Look for the DS1307 module, and search for matching libraries and tutorials.

pert:
We’d need to know more about what you’re trying to do and what hardware you’re using to do it. If you want the time and date when the sketch was compiled you can use the DATE and TIME macros:

void setup() {

Serial.begin(9600);
 while(!Serial){}
 Serial.println(“Compilation date:”);
 Serial.println(DATE);
 Serial.println("Compilation time: ");
 Serial.println(TIME);
}

void loop() {}




But that's only going to tell you when the sketch was uploaded to the board, not the current time/date after that.

How to catch those into variable, let’s say to char array or string for example?

chickchupie:
how to get the time and date using arduino in arduino version 1.8.1

GPS signal or NTP server are quite handy way to get exact time

TommiP

it overflows and resets after around 54 days.

...But that's not a problem if you follow some simple rules (sp. "49 days")

TommiP:
How to catch those into variable, let's say to char array or string for example?

The macros are string literals. If you want to put it in a char array you can do something like this:

void setup() {
  Serial.begin(9600);
  while(!Serial){}
  char compilationDate[] = __DATE__;
  char compilationTime[] = __TIME__;
  Serial.println("Compilation date:");
  Serial.println(compilationDate);
  Serial.println("Compilation time: ");
  Serial.println(compilationTime);
}

void loop() {}

pert:
The macros are string literals. If you want to put it in a char array you can do something like this:

void setup() {

Serial.begin(9600);
  while(!Serial){}
  char compilationDate = DATE;
  char compilationTime = TIME;
  Serial.println("Compilation date:");
  Serial.println(compilationDate);
  Serial.println("Compilation time: ");
  Serial.println(compilationTime);
}

void loop() {}

Thanks! (+1) :slight_smile:

TommiP