Link to home
Start Free TrialLog in
Avatar of CT2000
CT2000

asked on

How to calculate time difference using Lotus Script

Hi,
  I need to write a time sheet entry screen for my user.
On the form, I have the date of entry (field name: date), time start (field name: Startwork), Time end (field name:Endwork).
 
   Can anybody advise, how to calculate the time difference. I have the field timespent. To calculate the difference between Startwork and Endwork.

   Please advise.
   Thanks in advance
   CT2000
Avatar of Jean Marie Geeraerts
Jean Marie Geeraerts
Flag of Belgium image

I suppose all fields are date/time fields where you only display the time as hh:mm.

In this case put the following formula in your TimeSpent Date/time field :
Temp:=@if(Endwork="" | Startwork=""; ""; Endwork-Startwork);
@if(Temp="";""; @time(@integer(Temp/3600);(Temp-(@integer(Temp/3600))*3600)/60;0))

The first line will calculate the value in seconds.
The second line will convert the result back to a time value so it will display as hh:mm in your field.
Avatar of CRAK
I tested following formula in a button, assuming only the two time-fields were relevant. D1 and D2 are my NotesDateTime fields (time only; hours, minutes and seconds):

Sub Click(Source As Button)
     Dim WS As New NotesUIWorkspace
     Dim UIDoc As NotesUIDocument
     Dim Dt1 As NotesDateTime
     Dim Dt2 As NotesDateTime
     Dim Dt3 As NotesDateTime
     
     Set UIDoc = WS.CurrentDocument
     Set Dt1 = New NotesDateTime(UIDoc.FieldGetText("D1"))
     Set Dt2 = New NotesDateTime(UIDoc.FieldGetText("D2"))
     
     Set Dt3 = Dt2
     Call Dt3.AdjustHour(-Hour(Dt1.LsLocalTime))
     Call Dt3.AdjustMinute(-Minute(Dt1.LsLocalTime))
     Call Dt3.AdjustSecond(-Second(Dt1.LsLocalTime))
     Messagebox Dt3.LsLocalTime
End Sub
Why so complicated CRAK ? It can be done with a simple calculated field with the formula as stated in my comment above ;-)
Hey aren't you getting close to the TOP-15?
I would not have chosen lotusscript to solve this one myself, jerrith. I'd rather keep it simple and @-coded, but the question was to use Lotusscript (see subject header).
Quite a popular topic this week: date/time handling, isn't it?
Avatar of CT2000
CT2000

ASKER

Hi,
   Below is the model answer:

Sub Click(Source As Button)
     Dim x As New Notesuiworkspace
     Dim u As notesuidocument
     Set u = x.currentdocument
     Dim timetaken As  Double
     Dim Roundedtime As Double
     Dim texttime As String
     Dim Starttime  As NotesDatetime
     Dim EndTime As NotesDatetime
     Set StartTime = New NotesDatetime(u.fieldgetText("StartWork"))
     Set EndTime = New NotesDatetime (u.fieldgetText("EndWork"))
     timetaken =  ( endTime.TimeDifference (Starttime))  / 3600  
'     texttime =  @Round(timetaken; 1)
     
     Call u.fieldsettext("TimeSpent", texttime)
     Messagebox timetaken, , "timetaken"
End Sub

  One shortcoming is when the "timetaken" has been calculated it is having too many decimal places. I try to use the formula @Round. But it does not work! (     texttime =  @Round(timetaken; 1)) I need to round it to two decimal places.
    Please advise
    Thanks in advance.
    CT2000
"When using Lotusscripot, you can't use @-functions (unless you use Evaluate). Try "Round" (without "@") instead.
CRAK,
Yes, it does seem to be a popular subject this week.
I just ignored the question header and suggested what I thought was the best/simplest answer.
Anyway if CT2000 insists on using LotusScript he can use your code. As you said, I too would prefer to use @formula's in this case.

CT, to convert your value to a correct string :
texttime = format$(cint(timeTaken/3600), "00") + ":"  + format$((timeTaken-(cint(timeTaken/3600)*3600)))/60, "00")

You cannot use @formula's in lotusscript, unless you pass them to the script function "evaluate".
If you need to display time spent as a digital value, it's even simpler : texttime=format$(timetaken, "0.00")
What a team!
By the way jerrith: I only have 4460 pt in the Lotus Notes area. I only started Oct last year. Back then approx 5500 was nearly enough for a top 10 entry, but whenever I am to busy at work, you guys continue increasing the gap :-((
It that T-shirt worth it?
Yes, it's good quality. You have to be patient, though. I still haven't gotten my second one. Come to think of it, I think I'm gonna send the guys at EE a little reminder about that ;-)
I know what you mean, I've been a bit busy at work lately and before I know it Sloeber has overtaken me with some easy questions :-(
And to think that all he knows about the web he learned from me! Hihi
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.

____________________________________________



Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.

Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange

P.S.  REGARDING POINTS, Guidelines and more:  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  
In my opinion it would be fair to split points between CRAK and myself. Since we both provided a valid answer to the question.
ASKER CERTIFIED SOLUTION
Avatar of CRAK
CRAK
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CT2000

ASKER

Hi Expert Exchange team,
   Could you please split the point between the two experts
Hi, CT2000.

Let's see what the experts here say in that regard.  You have already chose a comment here and accepted it as the answer and graded to close it with a "C".  Few (if any) experts appreciate a "C" grade, and this question total was only 10 points.  "Easy" questions typically begin at a point value of 50.  

Will listen further when I can in this regard.

Moondancer
Community Support Moderator @ Experts Exchange
Yep, people giving C's get notes in my little black book ;-)

I'm not all that keen on putting a lot of time and effort in questions asked by somebody that frequently just gives a C, even though an expert has spent a reasonable amount of time to try to help.

I think it's a matter of principle. I nearly always give an A-grade, just to show my appreciation for the help offered. If I give a B, that will mean the answer I got was very lame and there was no better alternative. I never give a C myself. But I can understand that sometimes an expert doesnt deserve more than a C, however in this particular question I don't really think any of us deserved a C grade, do you?

Please don't take offense, this is just my opinion.
Are you sure?
I'd rather have a "C" on a 100pt question than an "A" on a 10pt one.... Get's me those T-shirts a bit faster!  ;-))

It's the challenge for me.... helping others and trying to do so better/faster than others. As long as things move towards a solution and not bleed to death for some reason!

After have done a real lot of effort, a "C" hurts a bit, but sometimes problems cannot be solved 100%. Would an "A" be fair in such case?
Solution: raise point first, then gade "C"....
Just gidding!

* signing off *