Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Calculating Time Difference

Hi everyone,

I have Time1 and Time2, how can I let Lotus Script to calculate the time difference automaticaly, like Time2 - Time1 ?

Cheers all...
0
Faraj1969
Asked:
Faraj1969
  • 2
2 Solutions
 
p_parthaCommented:
Set firstTime = New NotesDateTime(source.FieldGetText("Time1"))
Set secondTime = New NotesDateTime(source.FieldGetText("Time2"))

If secondtime.TimeDifference(firsttime) > 0 Then

nodayssecs=secondtime.TimeDifference(firsttime)
nodays=nodayssecs/86400


source is notesuidocument

Partha
0
 
Faraj1969Author Commented:
Thanks Partha,

Set Tfrom      = New NotesDateTime( UIDoc.FieldGetText( "Ndf_1" ) )       ' From Time
Set Tto      = New NotesDateTime( UIDoc.FieldGetText( "Ndt_1" ) )       ' To Time

Call UIDoc.FieldSetText( "Ndh_1", Trim( Cstr( Tto.TimeDifference( Tfrom ) / 86400 ) ) )

I entered 17:00 for Tfrom field and 19:00 for Tto field, the result was 8.33333333333333E-02
Ndh_1 is DateTime computed field.  The code worked but not the result I was looking for.

Any remedy ?

Thanks in advance.
0
 
qwaleteeCommented:
Tto.TimeDifference( Tfrom ) / 86400 returns the fraction of a day between the two.  It is returning exactly what you asked for -- but not in the way you want it to.

The actual difference is 2 hours, or 2*60=120 minutes, or 120*60=7200 seconds, which is what TimeDifference is actually retruning.  But, when you divide by a days' worth (86400) of seconds, you get 7200/86400, same as 2/24 hours, which is 8.33333333333333E -- otherwise known as as 0.083333333333333.  Absolutely accurate -- try dividing 2 by 24.

Partha was ASSUMING you wanted the difference in # of days.  WHat you really want is seconds, minutes, or hours. Or maybe you want a time format of hh:mm.

To get seconds, just leave off the /86400.  To get minutes, use /60 instead of 86400.  To get hours, use /3600 instead of /86400.

To get a nice time format, use:

allSeconds = ...Tto.TimeDifference( Tfrom )
hours = allSeconds\3600
remaining = allSeconds mod 3600
minutes = remaining\60
seconds = remaining mod 60
displayDif = hours & ":" & minutes & seconds
...FieldSetText "Ndh_1" , displayDif
0
 
Faraj1969Author Commented:
Thanks Qwaletee, thats what I needed, and sorry Partha, my explanation sucks at times, guess Qwaletee knows me... lol
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now