xsharp.eu • Datetime calculation store and subtract Starttime and Endtime Please Help
Page 1 of 1

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 12:26 pm
by rn@warner-it.com
Hi Guys,
Can someone please help me to code this so that it will work , I have a starttime and a endtime both in a label on a form, When I stop then I want to calculate the actualtime span between start and stop in a 3rd label. I have tried many ways but encounter errors like Datediff does not exist in current context. I have tried defining 2 fields to move the date to and then subtract this does not work either I cannot move the label date to the external fields. I woujld appreciate a example code in X# and which -Net classes do I need for this ??
Thanks my simple code is below.
Raymond


METHOD Stop_ButtonClick(sender AS System.Object , e AS System.EventArgs) AS VOID
LOCAL EndTime AS datetime
LOCAL Duration AS Timespan




oLabel3:Text := Time()
oTimer1:Enabled := FALSE
SELF:oLabel4.Text := Datediff("h",StartTime,endTime)
Messagebox.show(SELF:oLabel4.Text:ToString())

RETURN
METHOD Start_ButtonClick(sender AS System.ObMETHOD Stop_ButtonClick(sender AS System.Object , e AS System.EventArgs) AS VOID
LOCAL EndTime AS datetime
LOCAL Duration AS Timespan




oLabel3:Text := Time()
oTimer1:Enabled := FALSE
SELF:oLabel4.Text := Datediff("h",StartTime,endTime)
Messagebox.show(SELF:oLabel4.Text:ToString())

RETURNnt contextject , e AS System.EventArgs) AS VOID
LOCAL StartTime AS datetime

oTimer1:Enabled := TRUE
oLabel2:Text := Time()


RETURN
METHOD Stop_ButtonClick(sender AS System.Object , e AS System.EventArgs) AS VOID
LOCAL EndTime AS datetime
LOCAL Duration AS Timespan




oLabel3:Text := Time()
oTimer1:Enabled := FALSE
SELF:oLabel4.Text := Datediff("h",StartTime,endTime)
Messagebox.show(SELF:oLabel4.Text:ToString())

RETURN

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 12:29 pm
by rn@warner-it.com
Hi My name is Raymond
I am a Vo and Vulcan fan and moved to #Sharp I have with Dieter Crispen, John who I hear had passed of
I have also had contact with Chris Pygras in the past thanks for his help.
METHOD Stop_ButtonClick(sender AS System.Object , e AS System.EventArgs) AS VOID
LOCAL EndTime AS datetime
LOCAL Duration AS Timespan




oLabel3:Text := Time()
oTimer1:Enabled := FALSE
SELF:oLabel4.Text := Datediff("h",StartTime,endTime)
Messagebox.show(SELF:oLabel4.Text:ToString())

RETURNen

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 1:11 pm
by Chris
Hi Raymond!

In general, when you have 2 date time objects, you can substract them and get a TimeSpan object with represents their difference:

LOCAL oDuration AS TimeSpan
oDuration := DateTimeEnd - DateTimeStart

the oDuration object contains info about how many hours, minutes etc have passed, you can choose to create a string out of it manuall, or you can just use oDuration:ToString() directly.

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 2:08 pm
by rn@warner-it.com
Hi Chjris,
I just tried what you wrote and I got the following error
Operator "-" Cannot be applied to operands of type SystemDateType and Real8
By the way Chris how do I get the StartTime and EndTime
I want to do this : oLabel2:Text := Time()
StartTime := SELF:olabel2:Text
EndTime := SELF:olabel3:Text
Error cannot convert type string mto SystemdateTime
Chris I have tried all soughts of variations and have had no success can you please give me an example of what I want to do as shown here.
Then oDuration := EndTime - StartTime
I am afraid it does not work I get errors compiling what am I doing wrong ???


Thanks waiting for you reply
Raymond

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 2:58 pm
by Chris
Hi Raymond,

Just declare a PROTECT in your class:

PROTECT dtStart AS DateTime

When the process that you want to check starts, set

SELF:dtStart := DateTime.Now

when it ends:

LOCAL oDuration AS TimeSpan

oDuration := DateTime.Now - SELF:dtStart

now you can display the duration to your label. In other words, do not try to do calculations between label texts, instead just use a var that holds the starting time.

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 3:22 pm
by rn@warner-it.com
Hi Chris,
Yes this works Chris just a question I only want the time how can I define this so that I just get the startime and EndTime with datetime.now I get both the date and Time I just want the time that was the reason I used the time ()
Option.
Currently thios is defined as you suggested
PROTECT dtStart AS DateTime

Again thanks a lot Chris

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 5:50 pm
by lumberjack
Hi Raymond,
rn@warner-it.com wrote: Yes this works Chris just a question I only want the time how can I define this so that I just get the startime and EndTime with datetime.now I get both the date and Time I just want the time that was the reason I used the time ()
Unfortunately Time() will not work if your timespan goes past midnight. A DateTime object is your safest bet as Chris pointed out. Otherwise just extract the HH:MM:SS from the object and create a Time String object.

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Sun Feb 23, 2020 8:51 pm
by Chris
Raymond,
rn@warner-it.com wrote:Hi Chris,
Yes this works Chris just a question I only want the time how can I define this so that I just get the startime and EndTime with datetime.now I get both the date and Time I just want the time that was the reason I used the time ()
Option.
Currently thios is defined as you suggested
PROTECT dtStart AS DateTime

Again thanks a lot Chris
You can choose to use only the parts of the TimeSpan object that you are interested in. For example

cDuration := oDuration:Minutes:ToString() + " minutes, " + oDuration:Seconds:ToString() + " seconds"
(and then assign cDuration to the label text)

A more fancy way to do the same thing in .Net is:

cDuration := String.Format("{0} minutes, {1} seconds" , oDuration:Minutes, oDuration:Seconds)

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Tue Feb 25, 2020 10:01 am
by Karl-Heinz
i think what Raymond is looking for is the timepart of a DateTime :

SELF:dtStart:ToLongTimeString()

regards
Karl-Heinz

Datetime calculation store and subtract Starttime and Endtime Please Help

Posted: Tue Mar 03, 2020 9:48 am
by hans
Hi Raymond,
with VO I used the function ELAPTIME for such purposes. This function is also ported to X# as functions.Elaptime method. Maybe

Regards
Hans