Elapsed seconds

I am a Xojo beginner and am completely frustrated.

I have a date in a label and when a button is clicked, I need to calculate the seconds that have elapsed.
In other software I can use something like
ElapsedSeconds = DateDiff(Now, CDate(MyLable.Text), “ss”)
to get what I need and not 50 lines of code.

Just trying to convert a text/string back to a date to use the data stored in the label has proven to be too difficult for me. Can anyone point in the right direction?

Thanks in advance!

dim start as new date
..... do stuff
dim finish as new date
msgbox "elapsed time="+str(finish.totalseconds-start.totalseconds)

dang… 3 lines of code, including the notification to the user :slight_smile:

Some directions and tips to have fun and play for you to solve this on your own.

You can use timer object (see ref.).
Beside that you can use variable where you will store starting date & time and on certain time to get current time and make diff or you can use only counter where increment will goes each 1000 ms which is 1s and do the math without using certain starting date.

also @Dave S gives you code tip as well :slight_smile:

it’s easy.

have a fun mate.

cheers.

Dave: My “start” is stored in a label so Xojo thinks it is text, therefore “.totalseconds” gives me an error.

Bogdan: I have been trying to solve this on my own and have failed. This is no longer fun.

Dave’s code shows you how to compare two dates but is based on timing a process, which is not what you want. Your question, if I understand it, is how to convert a string into a Date object and back, right?

The answer is, it depends on the format of the string. If it’s in SQLDateTime format (YYYY-MM-DD HH:MM:SS), you can do this:

dim d1 as new Date
d1.SQLDateTime = myString

If it’s in localized format, whatever that is for your machine, you can use the ParseDate function.

remember ParseDate doesn’t do “time” (or at least it didn’t last time I looked)

meaning you granularity will be up to 23hours 59min off…

Ah, didn’t think about that (nor remember it :slight_smile: ). In that case, SQLDateTime format is the best bet.

If the string is just a double representing total seconds, you can do this:

dim d1 as new Date
d1.TotalSeconds = myString.Val

If it’s some other format, you have to parse it yourself.

Put timer, set on 1 sec. / 1000ms to execute timer event.
In part of timer event on every 1 sec. put a code which will update counter variable (which can be a global scope variable or some control on gui or even class object… ) which is n seconds counter e.g. nsec = nsec + 1
after that code you can also make label update and say somecontrol.text = nsec & " passed since start."
and extra code could be a part where you want to make calculation diff between date time when started and elapsed time in format of date and time.

Look xojo reference please!

Here is what I wrote years ago to get the difference between two dates:

[code]Protected Function getDateDifferenceWAD(startDate As date, endDate As date, includeTime As Boolean = True) as String
'returned the time difference between two strings as a text description
Dim tempReturn As String
Dim tempDate As New Date
Dim tempYears, tempMonths, tempDays, tempHours, tempMinutes, tempSeconds As Integer

if startDate = nil or endDate = nil then Return “”

tempDate.TotalSeconds = abs(endDate.TotalSeconds - startDate.TotalSeconds) 'tempInt 'tempDouble

tempYears = abs(tempDate.Year - 1904)
tempMonths = abs(tempDate.Month - 1)
tempDays = abs(tempDate.Day - 1)
tempHours = abs(tempDate.Hour)
tempMinutes = abs(tempDate.Minute)
tempSeconds = abs(tempDate.Second)

if tempYears <> 0 then
tempReturn = tempReturn + str(tempYears)
if tempYears = 1 then tempReturn = tempReturn + " year " else tempReturn = tempReturn + " years "
end if
if tempMonths <> 0 then
tempReturn = tempReturn + str(tempMonths)
if tempMonths = 1 then tempReturn = tempReturn + " month " else tempReturn = tempReturn + " months "
end if
if tempDays <> 0 then
tempReturn = tempReturn + str(tempDays)
if tempDays = 1 then tempReturn = tempReturn + " day " else tempReturn = tempReturn + " days "
end if
if includeTime then
if tempHours <> 0 then
tempReturn = tempReturn + str(tempHours)
if tempHours = 1 then tempReturn = tempReturn + " hour " else tempReturn = tempReturn + " hours "
end if
if tempMinutes <> 0 then
tempReturn = tempReturn + str(tempMinutes)
if tempMinutes = 1 then tempReturn = tempReturn + " minute " else tempReturn = tempReturn + " minutes "
end if
if tempSeconds <> 0 then
tempReturn = tempReturn + str(tempSeconds)
if tempSeconds = 1 then tempReturn = tempReturn + " second " else tempReturn = tempReturn + " seconds "
end if
end if

if endDate.TotalSeconds - startDate.TotalSeconds < 0 then tempReturn = “-” + tempReturn

Return tempReturn

End Function
[/code]

Thanks Guys! I got it working.

Dim SignalDate As New Date
SignalDate.sqldatetime = SignalTimeStamp.Text

Dim endTime As New Date
Dim elapsedSeconds as Double
elapsedSeconds = endTime.TotalSeconds - SignalDate.TotalSeconds

[quote=393935:@Bogdan Pavlovic]Put timer, set on 1 sec. / 1000ms to execute timer event.
In part of timer event on every 1 sec. put a code which will update counter variable (which can be a global scope variable or some control on gui or even class object… ) which is n seconds counter e.g. nsec = nsec + 1
[/quote]

A Timer, ironically, is not appropriate for keeping track of time. A period of 1 Second executes the Action event after at least 1 Second has elapsed; several seconds (or longer) might pass between executions depending on what your app is doing.

Xojo.Core.Date.FromText can parse a date including a timestamp.
I know it will be deprecated but it seems reasonable that Date will inherit the ToText method.

If it’s just seconds you need, using Date is overkill. I just use ticks (60 of them = 1 second).

dim start as double = ticks
// do stuff
dim elapsedSeconds as Double = (ticks - start) / 60