Converting String to Date:(Day, Month, Year, Time)

Quick question…

I have a string I receive from an external XML file that is formatted as a Date:

“Tue, 31 Jul 2012 02:35:00 GMT”

I would like to perform a function on that string to produce these results:

Day = “Tuesday”
Month = “July”
Year=“2012”
TimeGMT=“2:35 AM”
TimeEST=“11:35 PM”

For example… is there something similar to the following available in Corona?

DateString=“Tue, 31 Jul 2012 02:35:00 GMT”
Day = DatePart(DateString,“D”)
Month= DatePart(DateString,“M”)
Year= DatePart(DateString,“Y”)
TimeGMT= DatePart(DateString,“T”)
TimeEST= DateAdd (TimeGMT,-3) – Subtract 3 hours from Time

I can’t find any examples in the docs to extract “DateParts” from a string. Any suggestions?

Thanks in advance…

B [import]uid: 64343 topic_id: 29261 reply_id: 329261[/import]

Hi Byron,

There isn’t a built-in “date parts” function for Corona (at least none that I’ve heard about) but you should be able to write your own fairly easily. You can do so by using the string find or match functions, looking for certain patterns in your XML string.

For example, you would first look for three letters and a comma… this should return a result no matter what the day is. Then use an if-then-else series to output (return) that day in full name format. A match of “Mon,” would return Monday, “Thu,” would return Thursday, etc. Other parts of the date like month would be handled similarly, using match patterns. The entire function could be enhanced to almost crazy detail, accepting and returning parameters based on what you desire. It just depends how far you want to go with it… but certainly it’s not a difficult endeavor.

Here are the string APIs. I think .match and .find will meet your needs for this.
http://docs.coronalabs.com/api/library/string/index.html

Best regards,
Brent

[import]uid: 9747 topic_id: 29261 reply_id: 117660[/import]