Resources
Adafruit Learning System Adafruit DS3231 Precision RTC Breakout
The DS3231 keeps time even when Arduino is unplugged — it has its own crystal oscillator and backup coin cell battery.
Test Arduino Code
#include <RTClib.h>
#include <Wire.h>
DS3231 rtc;
char buf[20];
void setup() {
Serial.begin(9600);
while (!Serial);
Wire.begin(); // Nano 33 IoT uses Wire on SDA/SCL pins
rtc.begin();
if (!rtc.isrunning()) { // this method belongs to the DS3231 lib, recc'd by Arduino
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop() {
DateTime now = rtc.now();
Serial.println(now.tostr(buf)); // <-- using tostr(), not toString()
Serial.print(" since midnight 1970/1/1 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
DateTime future(now + (7 * 86400L + 30));
Serial.print(" now + 7d + 30s: ");
Serial.println(future.tostr(buf)); // <-- tostr()
DateTime past(now - 30 * 86400L);
Serial.print(" now - 30d: ");
Serial.println(past.tostr(buf)); // <-- tostr()
Serial.println();
delay(3000);
}