2017-02-17 166 views
0

我需要比較傳入字符串與DS1307 RTC模塊的日期和時間。如果到達字符串的特定時間,我將觸發一個事件。DateTime,字符串比較

我試過使用轉換爲整數,但它不起作用。

String now_int = rtc.now(); 

錯誤說conversion from DateTime to non-scalar type String is requested

我如何比較日期時間用字符串?

+0

'now()'返回一個'DateTime'對象而不是一個字符串或一個int。 –

+1

可能重複[如何將字符串轉換爲日期時間在C++](http://stackoverflow.com/questions/4781852/how-to-convert-a-string-to-datetime-in-c) – Shawn

+0

你可以發佈你完整的例子,特別是你打算執行的比較? – BNT

回答

0

您可以使用sprintfstrcmp的組合來實現您描述的行爲,例如,類似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 
} 

或者你根據自己的格式在arduino stackexchange描述了將您rtc.now()日期時間。