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

**URL:** <https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036>\
**Category:** Programming\
**Created:** [December 15, 2021, 12:08am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036 "2021-12-15T00:08:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlman69](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mlman69](https://forum.arduino.cc/u/mlman69)\
**Post date:** [December 15, 2021, 12:08am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036/1 "2021-12-15T00:08:40Z")

</div>

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  
Adafruit\_SSD1306 oled(SCREEN\_WIDTH, SCREEN\_HEIGHT, &Wire, OLED\_RESET);  
DateTime now;

void [color=#5E6D03]setup/color {  
Serial.[color=#D35400]begin/color;  
[color=#D35400]delay/color; // Wait for console opening  
initializeRTC();  
initializeOLED();  
}  
void [color=#5E6D03]loop/color {  
[color=#D35400]delay/color; // Update display every 100ms  
oled.setTextColor(WHITE, BLACK);  
oled.setTextSize(1);  
now = rtc.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.clearDisplay();  
oled.[color=#D35400]display/color;  
oled.setTextColor(WHITE, BLACK);  
oled.setTextSize(1);  
oled.setCursor(117, 55);  
oled.cp437(true);  
}  
void displayDay() {  
oled.setTextSize(2);  
oled.setCursor(3, 31);  
oled.[color=#D35400]print/color; // Display Day of the Week  
}  
void displayDate() {  
char currentDate [16];  
uint8\_t thisDay, thisMonth ;  
thisDay = now.day();  
thisMonth = now.month();  
sprintf (currentDate, "%02d/%02d/", thisMonth, thisDay); // Add leading zeros to month and day  
oled.setTextSize(2);  
oled.setCursor(6, 50);  
oled.[color=#D35400]print/color; // Display Date

oled.setTextSize(2);  
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.setTextSize(2);  
oled.setCursor(1, 1);  
oled.[color=#D35400]print/color; // Display Time

oled.setTextSize(2);  
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!

 ![DisplayTemplate](https://europe1.discourse-cdn.com/arduino/original/4X/e/7/c/e7c87550ddabb8af5f5872c735101ebaab745638.jpeg)  
 ![DisplayImage](https://europe1.discourse-cdn.com/arduino/original/4X/d/4/2/d4273abea3c7b6cd3f2dd68a62062006f34b7fd7.jpeg)

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [December 15, 2021, 12:26am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036/2 "2021-12-15T00:26:11Z")

</div>

Welcome

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 :

> <https://github.com/adafruit/RTClib/blob/master/src/RTClib.h>

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [December 15, 2021, 1:00am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036/3 "2021-12-15T01:00:55Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![mlman69](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@mlman69](https://forum.arduino.cc/u/mlman69)\
**Post date:** [December 15, 2021, 1:31am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036/4 "2021-12-15T01:31:27Z")

</div>

> [@guix](#):
>
> DateTime class

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 13, 2022, 1:31am UTC](https://forum.arduino.cc/t/reading-acting-on-a-ds3231-rtc-date-using-rtclib-h/936036/5 "2022-06-13T01:31:54Z")

</div>

Closed on June 13, 2022, 1:31am UTC

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