A Very Simple DateTime to String?

Hi everyone,

I'm trying to do something very simple: I'm trying to get a DateTime object and cast it into a String!

I expected that this would take me 2 minutes to do, but it's currently taken me 30 minutes!

The code is really simple:

  DateTime now = rtc.now();

  String str = String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " " + String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC);

  SerialTransmit("$RTC: " + str);

The output I expect is: "$RTC: 2017/12/01 13:13:55".
The output I receive is: "$RTC: 22165/165/165 165:165:85".

Does anyone know what is going on here?

:smiley:

if you print str to the console, what do you see?

what is SerialTransmit?

does addition overload support (const char *) + (String) ?

Don't use the String class at all would be the best advice

If you print now.month() to the serial console, what do you see?

J-M-L:
if you print str to the console, what do you see?

what is SerialTransmit?

does addition overload support (const char *) + (String) ?

Don't use the String class at all would be the best advice

If I print str to serial, I get: 2165/165/165 165:165:85

SerialTransmit is a wrapper method for Serial.println.

I assume it does support as there is no compiler error? I'm not 100% sure though.

If I print str to serial, I get: 2165/165/165 165:165:85

then fix this first (seems your RTC is not handled properly)

if you print now.year() - what do you get?
if you print String(now.year()) - what do you get?
if you print String(now.year(),DEC) - what do you get?

When a pump isn't dispensing water, always look at the intake. :slight_smile:

J-M-L:
then fix this first (seems your RTC is not handled properly)

if you print now.year() - what do you get?
if you print String(now.year()) - what do you get?
if you print String(now.year(),DEC) - what do you get?

I get junk, junk and more junk.

The strange thing is that I only tested this this on the Nano this morning and it was fine...

The problem is with the ESP32. The library doesn't seem to work with it.

For some reason, I assumed that the RTC library would work on ESP32. :smiley:

Or your connections aren't right, since you now have a different hardware configuration...

which RTC lib? and indeed which connexions? (and what voltage)

aarg:
Or your connections aren't right, since you now have a different hardware configuration...

SCL > SCL
SDA > SDA
VCC > 3.3v (5v doesn't work either)
GND > GND

Connections seem sound.

J-M-L:
which RTC lib? and indeed which connexions? (and what voltage)

RTCLIB by Adafruit. :slight_smile:

Load an I2C scan sketch and look for something on address 68 (DS3231).

aarg:
Load an I2C scan sketch and look for something on address 68 (DS3231).

Hey! Thanks for your help. In the end, I just used NTP. :slight_smile:

:slight_smile:

Just for anyone having similar problems, I had exactly the same issue and for me it turned out that the my breadboard wires were a little loose and not making contact properly. Turns out that the library still churns out these weird dates when you connect to a non-existent I2C address.

or I2C pull-ups missing?