Difference between two date/time fields - Lotus Notes - datetime

I have three editable date/time fields which the first two is (field1 and field2), style: Calendar/time control. Both of them are showing the time: hour and minutes, eg: 15:51.
The third field also (editable) which I want to show the difference between field1 and field2.
Eg: If field1 is 14:41 and field2 is 14:30, then field3 = 00:11. I've tried field1-field2 but isn't working. The form has automatic refresh fields property. Thanks!

Your third field needs to be computed, not editable.
If it HAS to be editable for some reason, and you want it to update when the other two fields are changed, do this:
Create a new field and make it computed-for-display and hidden. Give it a formula like this
#If(field1=null | field2=null; #Return(""); "");
seconds := field1-field2;
hours := #Integer(seconds/3600);
minutes := #Modulo(#Integer(seconds/60); 60);
output := #Right("00" + #Text(hours); 2) + ":" + #Right("00" + #Text(minutes); 2);
#setfield("field3"; output);
#Command([ViewRefreshFields]);
""
Phil

I wrote this code, much easier...
Fields 'StartTime' and 'EndTime':
Type Date/Time, use Calendar/Time control, set it to only display time.
Check the property "Run Exiting/OnChange events after value changes".
The Exiting event should look like this:
Sub Exiting(Source As Field)
Call UpdateDuration()
End Sub
Field 'Duration':
Editable text field, but hidden.
Field 'dspDuration':
Computed for display text field. Value is just "Duration" (no quotes).
Then add the following code to the forms Global section:
Sub UpdateDuration()
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim starttime As NotesDateTime
Dim endtime As NotesDateTime
Dim duration As Integer
Set uidoc = ws.CurrentDocument
'*** Exit if not both times are entered
If uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
Elseif uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
End If
'*** Calculate duration in seconds and update field
Set starttime = New NotesDateTime( uidoc.FieldGetText("StartTime") )
Set endtime = New NotesDateTime( uidoc.FieldGetText("EndTime") )
duration = endtime.TimeDifference( starttime )
Call uidoc.FieldSetText("Duration", Cstr(duration) )
Call uidoc.Refresh()
End Sub
That's it. Easy, isn't it? If you want to modify the output (duration), you can easily do that, perhaps change it into minutes by diving it by 60.

Make sure you are getting the difference between two date time fields. If you need to, you can use the #TextToTime formula to convert text to a datetime type.
Then just subtract the first date from the second date and you'll get the difference in seconds.
Then divide that by 60 to get the difference in minutes.

Related

Logic statement within a loop for ASP Classic

What would be the logic statement to notify the user that they purchased the same product within the last 90 days?
SQL = "SELECT id, PurchaseDate, ProductName from TableName where id = " & id)
Do Until objRS.EOF
if (objRS("ProductName") and objRS("PurchaseDate") <= dateadd(day,-90,getdate())) and objRS("ProductName") and isNull(objRS("PurchaseDate")) then
AlreadyPurchased = true
end if
objRS.movenext()
Loop
My loop that writes it to the page:
<%=objrs("ProductName")%><% if AlreadyPurchased then%>Last Purchased with last 90 days<%end if %>
The best answer depends on a few assumptions:
A) your query does not return any NULL values, and
B) that you have previously stored the product name in a preexisting variable.
Your code has some issues.
1) if (objRS("ProductName") is basically asking if the fieldset value is a True boolean value. My first assumption is that you already know the product name that you're testing. So, the above snippet should be replaced with this: if (objRS("ProductName").value = strMyProductName where strMyProductName is a variable that stores the product name (as a string).
2) You should consider storing the calculated date in a variable outside the loop. No need to repeatedly compute the date if you can compute it once, store it in a variable, and use the variable in your comparison.
3) Remove the last component of your conditional and objRS("ProductName") because it's redundant and has the same faulty logic as what I explained in (1) above.
4) Your DateAdd() can be written better.
TL;DR
dim strMyProductName, dat90, AlreadyPurchased
AlreadyPurchased = false
strMyProductName = "Shoes"
dat90 = dateadd("d", -90, now) '90 days ago
Do Until objRS.EOF
if cstr(objRS("ProductName").value)=strMyProductName and cdate(objRS("PurchaseDate")) <= dat90 then
AlreadyPurchased = true
exit do
end if
objRS.movenext()
Loop

MS Access calling a function

I am writing a programme for membership of a club in Access. Cell 1 will contain a number and cell 2 will contain cell 1 number in roman numerals.
I have a function that will convert the number but am having trouble getting cell 1 value into the function and the answer into cell 2. The start of the function is
Public Function RomanNumeral(ByVal aValue As Long) As String
and ends with
RomanNumeral = strResult
I would be very happy if anyone can help
Ok, this would be a calculated field. For this, you use =myFunction() as ControlSource.
In your case, if the number field has the name myNumber, use this for the roman number field:
=RomanNumeral([myNumber])
Edit
If you don't want a calculated field, but a field in the table, create an AfterUpdate event procedure for the number field, where you set the second field:
Private Sub myNumber_AfterUpdate()
' Use Nz to avoid runtime error when myNumber is NULL
Me!RomanNumber = RomanNumeral(Nz(Me!myNumber, 0))
End Sub

Displaying time difference in a lotus notes view

I have a computed field abc(of type Number) which holds the time difference between two dates and I have used this formula
#If(Start_time != "" & End_time != ""; #Round((ert - Untitled11)/ 3600) ; "0") ;
When I open the form, it shows me the difference in hours. Now I want the hours to be shown in a column in the view. Now in the column when I select the field as abc, it doesn't show any value in the column.
What can I do to display the hours in the view column?
After adding a field to a form in designer it is not automatically added to the documents that had been created before the field existed.
To do the calculation and add the item to the documents you have to open and save all documents or write an agent to refresh existing documents.
This agent can be a Formula agent, running on target none.
Formula: #Command([ToolsRefreshSelectedDocs]) or #Command([ToolsRefreshAllDocs])
Or it could be a LotusScript- Agent running on target All selected documents.
Code:
Dim ses as New NotesSession
Dim db as NotesDatabase
Dim dc as NotesDocumentCollection
Dim doc as NotesDocument
Set db = ses.CurrentDatabase
Set dc = db.Unprocesseddocuments
Set doc = dc.GetFirstDocument
While not doc is Nothing
Call doc.ComputeWithForm( False, False )
Call doc.Save( True, True, True )
Set doc = dc.GetNextDocument(dc)
Wend
After doing this, the item will be available in view columns.

VBScript - Database - Recordset - How to pass DateDiff value in the database

I'm using DateDiff() function of ASP to find the date difference between two dates.
The Function works fine and displays the exact date difference between two dates but when it comes to insert this value in the database it takes 9 as the value irrespect of any date difference.
Suppose difference between two dates is more than 15 or 20 days, in the database it takes "9".
I have used INT as the DATA TYPE for the column where it displaying the date difference.
Is DATA TYPE creating an issue here?
I even tried using session variable to store the value but no luck - here's my code below:
if request.Form("sub") <> "" then
sql = "Select * from emp_leave_details"
rs.open sql , con, 1, 2
dim diff
dim todate
dim fromdate
fromdate= rs("leave_from")
todate= rs("leave_to")
session("date_diff")=datediff("d",fromdate,todate)
rs.addnew
rs("emp_name") = request.Form("name")
rs("emp_no") = request.Form("number")
rs("address") = request.Form("address")
rs("contact_no") = request.Form("contact")
rs("mobile_no") = request.Form("mobile")
rs("contact_onleave") = request.Form("contact_details")
rs("leave_type") = request.Form("rad")
rs("other_leave_details") = request.Form("PS")
rs("leave_from") = request.Form("from")
rs("leave_to") = request.Form("to")
rs("applied_by") = request.Form("apply")
rs("accepted_by") = request.Form("accept")
rs("approved_by") = request.Form("approve")
rs("no_of_leave_taken")= session("date_diff")
rs.update
response.Write("<script language='javascript'>{update();}</script>")
rs.close
end if
The datatype has nothing to do with this. Storing the value in the session is not the solution. You can use a regular variable.
From your code it looks like you always use the same values for fromdate and todate. This is because you do not iterate the rows in the resultset.
if not rs.bof and not rs.eof then
do while not rs.eof
'' code to execute for each row
rs.moveNext
loop
end if
In your current script rs will always return the results of the first row returned by the query.
The second problem your running into might be the Date datatype. Convert your value to a date using cDate and use this to calculate the difference.
Your problem is , you search for "Select * from emp_leave_details" which always gives all records from that table. You retrieve the values of the first record and do a diff of these, which results in always the same value, that is normal. From your question it is unclear what you really want to do. I suppose so want so select a record like
Select * from emp_leave_details where emp_name=<%=request.Form("name")%>
and based on that add a new record with a computed no_of_leave_taken.
Sorry guys my bad...
It was the database field name that I was calling instead of
fromdate= request.form("from")
todate= request.form("to")
I was calling this
fromdate= request.form("leave_from")
todate= request.form("leave_to")
Sorry again..but I really appreciate you all for providing me with all that possible solutions.
Thanks.

Loop for each day in the current month and check rows for a match

How can I write a loop to run for each day in the current month and then check to see if the Dataset has a record that matches one of those days and take an action?
For Day As Integer = 1 To DaysInTheMonth
For Each row In MyRows
If row.Date.Day = Day Then
' Yup! Found Data for this day!
' Add Data to string
Else
' No Data To Display
' Add Blank Field String
End If
Next
Next
This generates too many rows in the table I have it building. Basically, every day gets as many rows as there are that contain data.
I'm building a HTML table that gets mapped into a chart using jquery so I need a for everyday of the month.
I think what would more suit your run would be having a day counter in the loop for the rows, so that you don't actually run over all the days again for each record in the datatable.
So now if you have the record its added to the string else skipped and the dat is incremented, so going down further you should get the required datapoints. give it a try
For Each row In MyRows
If row.Date.Day = Day Then
' Yup! Found Data for this day!
' Add Data to string
Else
' No Data To Display
' Add Blank Field String
End If
Day = Day + 1;
Next
You should drop the first For loop as that's causing all rows to be selected. Instead set Day = Today. Ie
Day as Integer = date.day

Resources