Getting the current date

How to:

Arduino itself doesn’t have a built-in method to directly fetch the current date, as it lacks a real-time clock (RTC). However, this can be achieved using external RTC modules like the DS3231, and libraries such as RTClib, developed by Adafruit, which makes interfacing with these modules straightforward.

First, ensure the RTClib library is installed in your Arduino IDE. Then, connect your RTC module to your Arduino according to its documentation.

Here’s a simple example to get you started:

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

void setup() {

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device or after a power loss, you can set it here.
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

void loop() {
  DateTime now =;

  Serial.print("Current Date: ");
  Serial.print(now.year(), DEC);
  Serial.print(now.month(), DEC);
  Serial.println(, DEC);

  delay(3000); // Delay for 3 seconds to reduce serial spam

Sample output (assuming your RTC has been previously set):

Current Date: 2023/4/15

This code initializes the RTC module and then, in the loop, fetches and prints the current date to the Serial Monitor every 3 seconds. Remember, the rtc.adjust(...) line can be uncommented and modified to set the RTC’s date and time initially or after it has lost power.