How do I convert timestamp to seconds

Get help using Construct 2

Post » Sat Apr 18, 2015 5:27 pm

Hi,

I was wondering if anyone could show me how to use the browser.execJS function to convert a MySQL timestamp into seconds? Simple expressions I'm good on, but this one is not so straightforward (at least to me).

So basically, I'd like to pass a timestamp as text (YYYY-MM-DD hh:mm:ss) and receive seconds back.

My intention is to find the time difference between two timestamps. So alternatively, if simpler, if might be easier to pass two timestamps and get the answer in seconds (timeA - timeB).

I hope someone can help.
Thanks
R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Sat Apr 18, 2015 6:41 pm

You could convert the timestamps into JavaScript Dates and subtract them:
http://stackoverflow.com/questions/3075 ... ate-format
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,620

Post » Sat Apr 18, 2015 8:15 pm

thanks ... so what you're saying is that if I convert it to JS DATE format (even prior to the function), then I can use JS date subtraction to accurately identify time differences (even with funny things like leap years and different month lengths?)

http://stackoverflow.com/questions/4944 ... javascript

var diff = Math.abs(new Date(dateStr1.replace(/-/g,'/'))- new Date(dateStr2.replace(/-/g,'/')));

which should turn "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.

R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Sat Apr 18, 2015 9:51 pm

Yeah that works well. timestamp_difference.capx (r200)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,620

Post » Sun Apr 19, 2015 8:40 am

thanks!!
B
7
S
3
G
1
Posts: 184
Reputation: 1,352


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 1 guest