Converting a date into a string

Arduino:
Converting a date into a string

How to:

Here’s a straightforward example of converting a date to a string on Arduino:

#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  
  DateTime now = rtc.now();
  char dateString[11]; // Enough space for "DD/MM/YYYY"

  sprintf(dateString, "%02d/%02d/%04d", now.day(), now.month(), now.year());
  Serial.println(dateString);
}

void loop() {
  // No need to repeat the conversion.
}

Sample output:

23/03/2023

Deep Dive

Historically, time representation has been a complex aspect of programming due to different formats and time zones. Arduino’s time-related functions take care of the complexity, allowing us to focus on making sense of the time data.

While we used the RTClib library, alternatives like the TimeLib.h offer similar functionality. Choosing one depends on preference and specific features, like built-in time zone handling.

The key function sprintf used here formats the data into a string. It’s based on the C standard library function, which is robust but can be memory-intensive for complex usage. A lighter, more basic alternative would be snprintf, which ensures you don’t exceed your buffer size and is safer against buffer overflows.

See Also