Using DateTime

I want to get the current time from my real time clock, change it, and load it back.

I can read it and write it with no problems, but I am having trouble changing the time:


I can get the hours field using now.hour(), but how can I change that value, say to 5 am? I've tried things like SetHour(5), etc.

The definition of the DateTime struct or class should make it clear how the components can be changed.

Yes, it should, but where is it?

Yes, it should, but where is it?

You installed the library. We can't tell where you installed it. We don't even know what operating system you are using.

if you use the RTClib you can use this function to set the date/time:
(just #include <RTClib.h> )

// set the RTC
void setDateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
  DateTime set = DateTime(year, month, day, hour, min, sec);

Should be self explanatory ... :wink:


Hi, thanks for your replies. I finally realized that DateTime is not part of C++ but defined right in RTClib.h, once I looked there I ended up doing something similar to what Pedro wrote.