Datetime calculation store and subtract Starttime and Endtime Please Help

We encourage new members to introduce themselves here. Get to know one another and share your interests.
Post Reply
rn@warner-it.com
Posts: 46
Joined: Mon Mar 04, 2019 4:41 pm

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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
rn@warner-it.com
Posts: 46
Joined: Mon Mar 04, 2019 4:41 pm

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
rn@warner-it.com
Posts: 46
Joined: Mon Mar 04, 2019 4:41 pm

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
rn@warner-it.com
Posts: 46
Joined: Mon Mar 04, 2019 4:41 pm

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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.
______________________
Johan Nel
Boshof, South Africa
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Datetime calculation store and subtract Starttime and Endtime Please Help

Post by Karl-Heinz »

i think what Raymond is looking for is the timepart of a DateTime :

SELF:dtStart:ToLongTimeString()

regards
Karl-Heinz
hans
Posts: 4
Joined: Fri May 19, 2017 1:32 pm

Datetime calculation store and subtract Starttime and Endtime Please Help

Post 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
Post Reply