【问题标题】:DateTime, String comparison日期时间,字符串比较
【发布时间】:2017-02-17 15:32:18
【问题描述】:

我需要将传入的字符串与来自 DS1307 RTC 模块的日期和时间进行比较。如果到达字符串中的特定时间,我将触发一个事件。

我尝试过使用转换为整数,但它不起作用。

String now_int = rtc.now();

错误提示conversion from DateTime to non-scalar type String is requested

如何比较日期时间和字符串?

【问题讨论】:

标签: c++ datetime time arduino real-time-clock


【解决方案1】:

您可以使用sprintf 和strcmp 的组合来实现您描述的行为,例如类似于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() DateTime。

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多