0

I need to compare incoming strings with the date and time from the DS1307 RTC module. I am going to trigger an event if the certain time from the strings are reached.

I've tried using convert to integer but it does not work.

String now_int = rtc.now();

Error says conversion from DateTime to non-scalar type String is requested

How can I compare datetime with a string?

BNT
  • 936
  • 10
  • 27
  • `now()` returns a `DateTime` object not a string or an int. – gre_gor Feb 17 '17 at 15:37
  • 1
    Possible duplicate of [How to convert a string to datetime in C++](http://stackoverflow.com/questions/4781852/how-to-convert-a-string-to-datetime-in-c) – Shawn Feb 17 '17 at 16:19
  • can you post your complete example, especially the comparison you are intending to perform? – BNT Jun 14 '17 at 11:03

1 Answers1

0

You could use a combination of sprintf and strcmp to achieve your described behaviour e.g. similar to this

// date and time from RTC
DateTime now = rtc.now();

// date and time to compare with - this is provided by you
String datetimeCompare = "1970/01/01 00:00:00";

// this buffer must be big enough for your complete datetime (depending on the format)
char datetimeBuffer[20] = "";

// convert current date and time to your specific format
sprintf(datetimeBuffer, "%04d/%02d/%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());

// perform the comparison
if(strcmp(datetimeBuffer, datetimeCompare.c_str()) == 0)
{
    // datetime strings are the same
}

Or you convert your rtc.now() DateTime according to your format as described over at the arduino stackexchange.

BNT
  • 936
  • 10
  • 27