# Extracting  hour/minute/second/date from String

**URL:** <https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065>\
**Category:** Programming\
**Created:** [February 29, 2016, 8:55pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065 "2016-02-29T20:55:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arsalh](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@arsalh](https://forum.arduino.cc/u/arsalh)\
**Post date:** [February 29, 2016, 8:55pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/1 "2016-02-29T20:55:07Z")

</div>

Can someone help me with converting a string to day/date/hour/sec/minute etc into separate variables?  
I have a string that contains date/time and want to use it to set time using Arduino's Time Library.

**STRING TO CONVERT:**"Date: Mon, 29 Feb 2016 12:02:00 GMT"  
**FUNCTION TO USE:** setTime(hr,min,sec,day,month,yr);

So from the string above, I want to extract hour, minute, seconds, day, month and year into variables. I am trying to do this on an Arduino Uno.

---

<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:** [February 29, 2016, 9:15pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/2 "2016-02-29T21:15:03Z")

</div>

Well, as you can imagine, it will be a huge pain. So, I have to ask, why do you need to read the time in this format? Perhaps we can suggest a better alternative if you explain the reason.

---

<div class="post-metadata">

**Author:** ![arsalh](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@arsalh](https://forum.arduino.cc/u/arsalh)\
**Post date:** [February 29, 2016, 9:29pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/3 "2016-02-29T21:29:02Z")

</div>

Hey I am trying to get accurate time for my project. I am using ESP8266 and want to keep track of the time in the project. I have seen the API methods for getting time but it seems to me that those take a little longer. So what I am doing instead is that I am extracting the time/date from the GET response I receive when I try to open a JSON file stored on a web server. Since I am opening the JSON file either way for my project, I wouldn't have to connect to a separate host to get time/date if I do it this way.

I may be super wrong in my assumptions but I just thought that might be the better way of approaching this. Let me know what you think.

---

<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:** [February 29, 2016, 9:47pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/4 "2016-02-29T21:47:54Z")

</div>

Oh, yeah. I've noticed that string. I thought about tapping into it. Have you thought about using NTP? There is an NTP example in the Ethernet library. The example shows how to convert the time to a format that the Time library can import.

Just be a good netizen and don't poll the NTP server more than once a few minutes.

Or even easier, use an accurate RTC.

Otherwise, I don't believe there is an easy answer, it will just be lines and lines of dull plodding parsing code.

Note that DateStrings.cpp in the Time Library has the strings you need, e.g.

```cpp
const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec";

```

---

<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:** [February 29, 2016, 11:06pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/5 "2016-02-29T23:06:42Z")

</div>

> Can someone help me with converting a string to day/date/hour/sec/minute etc into separate variables?

Is the data in a string, as this question suggests? Or, is it in a String, as the thread topic suggests.

Parsing a string is easy. Parsing a String is a pain in the ass.

---

<div class="post-metadata">

**Author:** ![Jon\_White](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jon\_White](https://forum.arduino.cc/u/Jon_White)\
**Post date:** [March 1, 2016, 12:42am UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/6 "2016-03-01T00:42:50Z")

</div>

Try this. As long as the date is always well-formed, it should work...

```cpp
#include "stdafx.h"

int ParseDatePart(int partNumber, char* pDate)
{
	// Find the right part using space and colon as delimiters.  Return -1 if we run out of parts.
	int index = 0;
	while (partNumber)
	{
		if (pDate[index] == ':' || pDate[index] == ' ') partNumber--;
		if (!pDate[++index]) return -1;
	}

	// If the part starts with a digit, read all of the digits.
	int result = 0;
	while (pDate[index] >= '0' && pDate[index] <= '9')
	{
		result = result * 10 + pDate[index++] - '0';
		if (pDate[index] < '0' || pDate[index] > '9') return result;
	}

	// Otherwise, read it as a month.
	switch (pDate[index++])
	{
	case 'J':
		if (pDate[index] == 'a') return 1;
		if (pDate[index++] && pDate[index] == 'n') return 6;
		return 7;
	case 'F':
		return 2;
	case 'M':
		if (pDate[index++] && pDate[index] == 'r') return 3;
		return 5;
	case 'A':
		if (pDate[index] == 'p') return 4;
		return 8;
	case 'S':
		return 9;
	case 'O':
		return 10;
	case 'N':
		return 11;
	case 'D':
		return 12;
	}

	// Return -1 if it didn't parse as either a number or a month.
	return -1;
}

int main(int argc, char** argv)
{
	if (argc < 2)
	{
		printf("Usage:  foo <date>\r\n");
		printf("Example: foo \"Date: Mon, 29 Feb 2016 12:02:00 GMT\"\r\n");
		return 1;
	}
	char* pDate = argv[1];
	int day = ParseDatePart(3, pDate);
	int month = ParseDatePart(4, pDate);
	int year = ParseDatePart(5, pDate);
	int hour = ParseDatePart(6, pDate);
	int minute = ParseDatePart(7, pDate);
	int second = ParseDatePart(8, pDate);
	printf("%d/%d/%d %d:%d:%d\r\n", month, day, year, hour, minute, second);
	return 0;
}

```

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [March 1, 2016, 4:11am UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/7 "2016-03-01T04:11:33Z")

</div>

> So from the string above, I want to extract hour, minute, seconds, day, month and year into variables. I am trying to do this on an Arduino Uno.

If the character string received is captured as a String, then the various String functions can be used to extract the desired data.

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [March 1, 2016, 2:38pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/8 "2016-03-01T14:38:56Z")

</div>

**Don't use `String`™**. The solutions using C strings (i.e., `char` arrays) are very efficient.

`String` will add at least 1600 bytes of FLASH to your program size and 10 bytes per `String` variable. Although `String` is fairly easy to use and understand, it will also cause random hangs and crashes as your program runs longer and/or grows in size and complexity. This is due to [dynamic memory](https://en.wikipedia.org/wiki/C_dynamic_memory_allocation) [fragmentation](https://en.wikipedia.org/wiki/Fragmentation_(computing)) caused by the heap management routines `malloc` and `free`.

The most recent example of the pitfalls of `String` can be seen [here](http://forum.arduino.cc/index.php?topic=380172.0).

Related forum discussions:

- [Optimizing memory without Strings](http://forum.arduino.cc/index.php?topic=353834.0),

- [Is String still bad?](http://forum.arduino.cc/index.php?topic=364772.0)

- [Dynamic memory forbidden in avionics systems](http://www.eevblog.com/forum/microcontrollers/own-malloc-implementation-metal-bare-looking-for-sources/) (eevblog)

- [Why shouldn't we have dynamic allocated memory... in embedded systems?](http://stackoverflow.com/questions/21370410)

- [Is using malloc and free a really bad idea on Arduino?](http://arduino.stackexchange.com/questions/682)

- [What is memory fragmentation?](http://stackoverflow.com/questions/3770457)

- [Struggling with manipulating data from serial buffer](http://forum.arduino.cc/index.php?topic=365955.msg2523224#msg2523224)  
Commentary from systems experts elsewhere on the net:

- [The Evils of Arduino Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/) **(required reading!)**

- [Why is malloc harmful in embedded systems?](https://www.quora.com/Why-is-malloc-harmful-in-embedded-systems)

- [Dr Dobbs Journal](http://www.drdobbs.com/embedded-systems/embedded-memory-allocation/240169150)

- [Memory Fragmentation in servers](http://blogs.msdn.com/b/david.wang/archive/2006/02/14/more-on-virtual-memory-memory-fragmentation-and-leaks-and-wow64.aspx) (MSDN)

- [Memory Fragmentation, your worst nightmare](http://library.softwareverify.com/memory-fragmentation-your-worst-nightmare/) (nice graphics)  
Cheers,  
/dev

---

<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:45pm UTC](https://forum.arduino.cc/t/extracting-hour-minute-second-date-from-string/369065/9 "2021-05-05T21:45:33Z")

</div>

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


