Convert string to date

Hi,

I have a string on this format: “2013-01-01T00:00:00Z”

How can I convert the string to a date?
How can I identify how many days have passed since that date?

Thanks [import]uid: 189638 topic_id: 34760 reply_id: 334760[/import]

basically you have to parse the string into its component numbers, but its really not that hard. Here is a function I use for it:

function M.makeTimeStamp(dateString)  
 local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)([%+%-])(%d+)%:(%d+)"  
 local xyear, xmonth, xday, xhour, xminute,   
 xseconds, xoffset, xoffsethour, xoffsetmin = dateString:match(pattern)  
 local convertedTimestamp = os.time({year = xyear, month = xmonth,   
 day = xday, hour = xhour, min = xminute, sec = xseconds})  
 local offset = xoffsethour \* 60 + xoffsetmin  
 if xoffset == "-" then offset = offset \* -1 end  
 return convertedTimestamp + offset  
end  

This looks much scarier than it really is. The goal is to turn that string into a Unix timestamp (number of seconds since Jan 1, 1970, the standard used by most systems). So we use the string.match() method to fetch the various date and time parts into their own variables: xyear, xmonth,etc.

Next we use the API call os.time() to convert all those individual parts into the timestamp. My code tries to adjust for time zones…

Now you have the time in a nice integer that you can easily manipulate and do date math. Now to determine how many days have passed since that date:

then = makeTimeStamp("2013-01-01T00:00:00Z")  
now = os.time()  
timeDifference = now - then  
daysDifference = math.floor(timeDifference / (24 \* 60 \* 60)) -- 24 hours, 60 min, 60 seconds. I think it works out to 86400 seconds in a day or something like that...  

[import]uid: 199310 topic_id: 34760 reply_id: 138198[/import]

basically you have to parse the string into its component numbers, but its really not that hard. Here is a function I use for it:

function M.makeTimeStamp(dateString)  
 local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)([%+%-])(%d+)%:(%d+)"  
 local xyear, xmonth, xday, xhour, xminute,   
 xseconds, xoffset, xoffsethour, xoffsetmin = dateString:match(pattern)  
 local convertedTimestamp = os.time({year = xyear, month = xmonth,   
 day = xday, hour = xhour, min = xminute, sec = xseconds})  
 local offset = xoffsethour \* 60 + xoffsetmin  
 if xoffset == "-" then offset = offset \* -1 end  
 return convertedTimestamp + offset  
end  

This looks much scarier than it really is. The goal is to turn that string into a Unix timestamp (number of seconds since Jan 1, 1970, the standard used by most systems). So we use the string.match() method to fetch the various date and time parts into their own variables: xyear, xmonth,etc.

Next we use the API call os.time() to convert all those individual parts into the timestamp. My code tries to adjust for time zones…

Now you have the time in a nice integer that you can easily manipulate and do date math. Now to determine how many days have passed since that date:

then = makeTimeStamp("2013-01-01T00:00:00Z")  
now = os.time()  
timeDifference = now - then  
daysDifference = math.floor(timeDifference / (24 \* 60 \* 60)) -- 24 hours, 60 min, 60 seconds. I think it works out to 86400 seconds in a day or something like that...  

[import]uid: 199310 topic_id: 34760 reply_id: 138198[/import]

I’m looking for a variation on this, in effect the reverse.
I know about os.time and converting to day, month, hour etc.
My problem is that I want to subtract from the current time and still have valid hours, days, months and even (once a year) year.
I’m building filenames of images that I grab from a server, one is written every 15 minutes.
A crude way is to get current day, hour etc. and do lots of if else statements to determine when I’m in previous hour, day,month or year.

I don’t think I can manipulate the input to os.date or os.time, can I? [import]uid: 120570 topic_id: 34760 reply_id: 141021[/import]

@conor1, please check out this blog post and see if it help you:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/
[import]uid: 199310 topic_id: 34760 reply_id: 141126[/import]

@rob
I’d already seen that and it explains the subject well.
What I need is some means of subtracting set amounts of time from the current time and having it automatically adjust hour, day, month and year when appropriate.

I don’t think there’s a way to do os.time(the time 15 minutes ago) is there?

[import]uid: 120570 topic_id: 34760 reply_id: 141145[/import]

Further issue.
print( os.date("%FT%X%z") ) – This crashes the simulator, no error code (using windows)

strftime listed here does not have a %F
http://www.cplusplus.com/reference/ctime/strftime/

Listed here it does.
http://php.net/manual/en/function.strftime.php

Also noticed that %D %R %T do the same. Am I missing something?
This is happening with 971 and 1019. [import]uid: 120570 topic_id: 34760 reply_id: 141167[/import]

@conor1
You can still use the [text]t[/text] table to subtract values.
It will work even if the result becomes a negative value:

local t = os.date('\*t'); -- get current date and time  
print(os.date("%Y-%m-%d %H:%m:%S", os.time(t)));  
t.min = t.min - 9000; -- subtract 9000 minutes  
print(os.date("%Y-%m-%d %H:%m:%S", os.time(t)));  

Output:
[text]
2013-02-01 18:02:31
2013-01-26 12:01:31
[/text] [import]uid: 70847 topic_id: 34760 reply_id: 141171[/import]

Great, with just a slight correction

local t = os.date('\*t'); -- get current date and time print(os.date("%Y-%m-%d %H:%M:%S", os.time(t)));
Capital M for minutes.

Thanks for that.
[import]uid: 120570 topic_id: 34760 reply_id: 141172[/import]

Ooooops! Typo alert! :wink:
Sorry about that…
[import]uid: 70847 topic_id: 34760 reply_id: 141173[/import]

So, just to give something back.
Here is code to calculate times going back every 15 minutes, rounded to nearest 15 minutes.
Still puzzled by @rob and the issue of %F etc.

[code]
local function makeDate(date)
if string.len(date)==1 then date=“0”…date end
return date
end

local date = os.date( “*t” )
date.min = date.min - 15;
t1=os.date("%Y%m%d%H", os.time(date))…makeDate(math.floor(os.date("%M", os.time(date))/15)*15)
date.min = date.min - 15;
t2=os.date("%Y%m%d%H", os.time(date))…makeDate(math.floor(os.date("%M", os.time(date))/15)*15)

[/code] [import]uid: 120570 topic_id: 34760 reply_id: 141174[/import]

print(os.date("%FT%X%z"))

According to the strftime man page, %F is the same as: %Y-%m-%d (the ISO 8601 date format).

http://linux.die.net/man/3/strftime

I’m guessing that Windows may have a different definition for strftime and maybe %F isn’t supported in Visual C++'s libraries. But you can sub the %Y-%m-%d in it’s place.
[import]uid: 199310 topic_id: 34760 reply_id: 141373[/import]

I’m looking for a variation on this, in effect the reverse.
I know about os.time and converting to day, month, hour etc.
My problem is that I want to subtract from the current time and still have valid hours, days, months and even (once a year) year.
I’m building filenames of images that I grab from a server, one is written every 15 minutes.
A crude way is to get current day, hour etc. and do lots of if else statements to determine when I’m in previous hour, day,month or year.

I don’t think I can manipulate the input to os.date or os.time, can I? [import]uid: 120570 topic_id: 34760 reply_id: 141021[/import]

@conor1, please check out this blog post and see if it help you:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/
[import]uid: 199310 topic_id: 34760 reply_id: 141126[/import]

@rob
I’d already seen that and it explains the subject well.
What I need is some means of subtracting set amounts of time from the current time and having it automatically adjust hour, day, month and year when appropriate.

I don’t think there’s a way to do os.time(the time 15 minutes ago) is there?

[import]uid: 120570 topic_id: 34760 reply_id: 141145[/import]

Further issue.
print( os.date("%FT%X%z") ) – This crashes the simulator, no error code (using windows)

strftime listed here does not have a %F
http://www.cplusplus.com/reference/ctime/strftime/

Listed here it does.
http://php.net/manual/en/function.strftime.php

Also noticed that %D %R %T do the same. Am I missing something?
This is happening with 971 and 1019. [import]uid: 120570 topic_id: 34760 reply_id: 141167[/import]

@conor1
You can still use the [text]t[/text] table to subtract values.
It will work even if the result becomes a negative value:

local t = os.date('\*t'); -- get current date and time  
print(os.date("%Y-%m-%d %H:%m:%S", os.time(t)));  
t.min = t.min - 9000; -- subtract 9000 minutes  
print(os.date("%Y-%m-%d %H:%m:%S", os.time(t)));  

Output:
[text]
2013-02-01 18:02:31
2013-01-26 12:01:31
[/text] [import]uid: 70847 topic_id: 34760 reply_id: 141171[/import]

Great, with just a slight correction

local t = os.date('\*t'); -- get current date and time print(os.date("%Y-%m-%d %H:%M:%S", os.time(t)));
Capital M for minutes.

Thanks for that.
[import]uid: 120570 topic_id: 34760 reply_id: 141172[/import]

Ooooops! Typo alert! :wink:
Sorry about that…
[import]uid: 70847 topic_id: 34760 reply_id: 141173[/import]