Reading & Acting on a DS3231 RTC Date using rtclib.h

I am trying to read a date and act if the date matches but have no idea how to do it. If possible, I would like to do this using rtclib commands directly. I have found nothing that would help anywhere and thought this forum might be my best shot. Code I am using is as follows:
/* Display_Sketch_96inAMPM.ino - 0.96" 128X64 OLED Display, DS3231 RTC and Arduino Nano
*********************** ADD SERIAL PRINT AT YOUR OWN RISK!

Arduino Nano, OLED and DS3231 RTC
Nano Pin A4 to SDA (DS3231 and OLED)
Pin A5 to SCL (DS3231 and OLED)
Pin 5V to DS3231 and OLED
Pin GND to DS3231 and OLED
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include "RTClib.h"
RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {" Sunday ", " Monday ", " Tuesday ", "Wednesday ", " Thursday ", " Friday ", " Saturday "};
boolean LoopOnceF = false;
#define SCREEN_WIDTH 128 // OLED display size in pixels
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
DateTime now;

void [color=#5E6D03]setup/color {
[color=#D35400]delay/color; // Wait for console opening
void [color=#5E6D03]loop/color {
[color=#D35400]delay/color; // Update display every 100ms
oled.setTextColor(WHITE, BLACK);
now =;

// Can't figure out how to read the date to display Happy Birthday all day once a year??????

// if !birthday
oled.setCursor(20, 20); // Cursor 20,20 for Hi
oled.print("Hi Gavin! It's");
// else birthday
// oled.setCursor(2,20); // Cursor 2,20 for Happy
// oled.print("Happy Birthday Gavin!");

displayDay(); // Display Day
displayDate(); // Display Date
displayTime(); // Display Time
void initializeRTC() {
if (! rtc.[color=#D35400]begin/color) {
Serial.println("Couldn't find RTC");
while (1); // Infinite loop
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
void initializeOLED() {
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(WHITE, BLACK);
oled.setCursor(117, 55);
void displayDay() {
oled.setCursor(3, 31);
oled.[color=#D35400]print/color; // Display Day of the Week
void displayDate() {
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay =;
thisMonth = now.month();
sprintf (currentDate, "%02d/%02d/", thisMonth, thisDay); // Add leading zeros to month and day
oled.setCursor(6, 50);
oled.[color=#D35400]print/color; // Display Date

oled.setCursor(78, 50);
oled.print(now.year(), DEC); // Display Year
void displayTime() {
char buffer [16];
char AmPm[3];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();

if (thisHour > 12) { // Convert 24 hour format to AM PM
thisHour = thisHour - 12;
strcpy(AmPm, "PM");
else strcpy(AmPm, "AM");

sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec); // Add leading zeros to the hour. minute and second

oled.setCursor(1, 1);
oled.[color=#D35400]print/color; // Display Time

oled.setCursor(103, 1);
oled.[color=#D35400]print/color; // Display AM or PM
oled.[color=#D35400]display/color; // Display everything in buffer

The display and display layout follows. Any help would be greatly appreciated!


There are multiple ways to compare dates

You can compare two DateTime objects, or you can convert them to unix timestamps and compare these...

Look at the methods of the DateTime class :

Since you are only concerned with the month and day, and not the year or time of day, then it would probably be easiest to just check for a matching day and month:

  const uint8_t birthdayMonth = 12;
  const uint8_t birthdayDay = 14;
  oled.setCursor(2, 20);
  if ((birthdayMonth == now.month()) && (birthdayDay == {
    oled.print(F("Happy Birthday Gavin!"));
  } else {
    oled.print(F("   Hi Gavin! It's    "));

Thank you very much! It was just what I was looking for.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.