# Comparing dateTime's

**URL:** <https://forum.arduino.cc/t/comparing-datetimes/388194>\
**Category:** Programming\
**Created:** [May 26, 2016, 12:26am UTC](https://forum.arduino.cc/t/comparing-datetimes/388194 "2016-05-26T00:26:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hawkagent](https://avatars.discourse-cdn.com/v4/letter/h/41988e/32.png) [@hawkagent](https://forum.arduino.cc/u/hawkagent)\
**Post date:** [May 26, 2016, 12:26am UTC](https://forum.arduino.cc/t/comparing-datetimes/388194/1 "2016-05-26T00:26:31Z")

</div>

Consider two strings

char dt1 []= "2016-05-14 15:23:00";  
char dt2 []= "2016-05-13 19:11:32";

What would be a good way to test which is newer?

I've looked up [Arduino Playground - Time](http://playground.arduino.cc/Code/Time) but it seems this library is designed for time-keeping and is only capable of keeping one instance of time.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [May 26, 2016, 12:29am UTC](https://forum.arduino.cc/t/comparing-datetimes/388194/2 "2016-05-26T00:29:48Z")

</div>

In that date time format, strcmp.

edit: That is assuming the day will also be a two digit number.

---

<div class="post-metadata">

**Author:** ![hawkagent](https://avatars.discourse-cdn.com/v4/letter/h/41988e/32.png) [@hawkagent](https://forum.arduino.cc/u/hawkagent)\
**Post date:** [May 26, 2016, 12:58am UTC](https://forum.arduino.cc/t/comparing-datetimes/388194/4 "2016-05-26T00:58:24Z")

</div>

I'm glad I asked here before inventing too complicated things. Because of the format, strcmp worked indeed. Thanks for the both suggestions!

```cpp
if (strcmp(dt1, dt2) < 0) {
		Serial.println("d1 is older than d2");
	}
	else {
		Serial.println("d1 is newer than d2");
	}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 26, 2016, 1:28am UTC](https://forum.arduino.cc/t/comparing-datetimes/388194/5 "2016-05-26T01:28:18Z")

</div>

Date/time in lexical order was designed for such comparisons and sorts. But interval calculations aren't as straightforward as they are with epoch time.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:54pm UTC](https://forum.arduino.cc/t/comparing-datetimes/388194/6 "2021-05-05T21:54:53Z")

</div>

Closed on May 5, 2021, 9:54pm UTC


