PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → [WD23] DateTime / Duration
[WD23] DateTime / Duration
Iniciado por guest, 28,may. 2018 08:30 - 3 respuestas
Publicado el 28,mayo 2018 - 08:30
Hi,
there's an unexpected difficulty (only for me?) in calculating a duration. The problem is simple.

A Date item (defined for YYYYMMDDhhmmssccc) of the file HISTORY receives the Log-in Date and Time:
GPW_HISTORY.HISTORY_LOGIN = DateToString(DateSys(),"YYYYMMDD")+TimeToString(TimeSys(),"HHMMSS")

Same goes for a logout. Whenever the main menue is closed then the log-out is recorded in another Date item
GPW_HISTORY.HISTORY_LOGOUT = DateToString(DateSys(),"YYYYMMDD")+TimeToString(TimeSys(),"HHMMSS")

Of course, there will be no logout Date+Time if the program crashes somewhen or as long as the user's session has not been terminated.

A table control shows both items correctly in their columns (defined as Dates, returning YYYYMMDDhhmmss). One could be happy with that, but now I want to show the exact time which elapsed between a Log-in and Log-out in a third column of the table.

I created a column with type of Duration, which will return DHHMMSSLLL and displays +2d d H h MM m SS s CCC ms. Using DateTimeDifference(..) of the LOGOUT / LOGIN datetime columns shows incorrect results! Does any of you know what's going on here or is there a solution??

The code
IF Table.HISTORY_LOGIN <> Null AND Table.HISTORY_LOGOUT <> Null THEN DateTimeLOGIN = Table.HISTORY_LOGIN DateTimeLOGOUT = Table.HISTORY_LOGOUT MyDuration = DateTimeDifference(DateTimeLOGIN,DateTimeLOGOUT) Table.DAUER = MyDuration ELSE Table.DAUER = 0 END [attachment 2712 LoginDauer.png]
Publicado el 28,mayo 2018 - 09:24
Hi Günter,

seems to be a formatting Problem only.

The values are correct, for example the first two lines:

01s300ms (should be 0 minutes and 13 seconds)
20s500ms (should be 2 minutes and 05 seconds)

Shifting one Position to the right and changing s to minutes and milliseconds to seconds
will give the correct values.

Hope this helps

Greetings

Erik
Publicado el 28,mayo 2018 - 10:57
Hi Erik, thank you! Yes, I saw that ... after some tweaking the code the solution looks quite simple like that:

DateTimeLOGIN = GPW_HISTORY.HISTORY_LOGIN DateTimeLOGOUT = GPW_HISTORY.HISTORY_LOGOUT MyDuration = DateTimeDifference(DateTimeLOGIN,DateTimeLOGOUT)+"0" Table.DAUER = MyDuration
Thank you!
Publicado el 28,mayo 2018 - 20:58
Hi Guenter,

yes the extra "0". I just remember that some time functions Need this, because they are counting the milliseconds, which Needs 3 Digits while most other (seconds, hours and so on have only 2 Digits).

Have a nice evening.

Erik