Function to compare dates

Hey!

I need a little help.

There’s a function to compare two dates in lotusscript?

Let me explain:

I have two text fields on a web application and I get this dates and make a comparation to run the agent. I know that compare two date- text strings are impossible, so I tried to make it with LSLocalTime.

But I got errors, a lot of them. Could you help me to resolve this pluzze?!

Thanks in advance.

Here is the code:

Use “WorkflowProcedures”

Sub Initialize

MsgBox "Agente MediaDia - Entrou no agente!"



On Error GoTo TrataError



Dim ses 		As New NotesSession

Dim db 			As NotesDatabase

Dim vis 		As NotesView

Dim col 		As NotesDocumentCollection

Dim doc 		As NotesDocument

Dim autor 		As Variant

Dim criacao		As Variant

Dim dataHoje	As String

Dim total       As Long



dataHoje = Format(Now, "mm/dd/yyyy")



MsgBox "Data Hoje: " + CStr(datahoje)



Set Webdoc  = ses.DocumentContext

Set db		= ses.CurrentDatabase

Set vis 	= db.GetView("Autores")

Set col 	= db.Alldocuments

Set doc 	= col.Getfirstdocument()



'MsgBox "Autor do primeiro documento da colection: " + doc.Authors(0)



total = col.Count



total = 0



While not doc Is nothing 

	

	datadoc = doc.Created()

	

	Dim data As New NotesDateTime(dataHoje)

	

	Dim data1 As New NotesDateTime(datadoc) 

	MsgBox "Data Hoje: " + CStr(dataHoje) + " Data do Doc: " + CStr(datadoc)

	

	If data.LSLocalTime = data1.LSLocalTime Then

	'If datadoc = datahoje Then

		MsgBox "**************************** Entrou no if *************************"

		

		total = total + 1

		

	End If

	

	MsgBox " Contador: " & total

	

	MsgBox "Data Criação dessa parada: " + CStr(Format(datadoc, "mm/dd/yyyy"))

	

	Set doc 	= col.Getnextdocument(doc)

Wend 







If total = 0  then 

	

	Print "<script language=""JavaScript"" type=""text/javascript"">"

	

	'Print |var path = window.location.pathname.substring (0, window.location.pathname.toLowerCase().indexOf(".nsf") + 4);|

	

	Print "alert('Não foram criados documentos de migrações hoje!" ".')";

	

	MessageBox "Não foram criados documentos de migrações hoje!"

	

	



	

Else

	

	Print "<script language=""JavaScript"" type=""text/javascript"">"



	'Print |var path = window.location.pathname.substring(0, window.location.pathname.toLowerCase().indexOf(".nsf") + 4);|



	Print "alert('Média de Migrações realizada durante o dia: " + CStr(total) + "" + ".')";

		

	MessageBox "Média de Migrações realizada durante o dia: " & CStr(total)





	

End If



	Print "</script>"

	

	Exit sub

TrataError:

MsgBox "Erro: " +  Error + Chr(10)+ " na Linha " + CStr(Erl) +  Chr(10)+ "numero " + CStr(Err) 

End Sub

Subject: Isolate your code…

First of all, you should isolate the code you want to test, then you run it in the debugger in Notes, before you create an agent on the server to be called from the web.

I would remove all the MsgBox statements, as that will fill up the server log very quickly… And it makes your code harder to read.

I also consider it best practice to declare all variables at the beginning of each function/sub, not all over the code…

And you should add “Option Declare” at the beginning, that will help youfind things liek undeclared vriables. I can’t see where datadoc is defined, for example…

That said, the code below should work:

Dim doc as NotesDocument

Dim currentdate As NotesDateTime

Dim docdate As NotesDateTime

… set doc here…

Set currentdate = New NotesDateTime(Now())

Set docdate = New NotesDateTime(doc.Created)

If docdate.DateOnly < currentdate.DateOnly Then

’ Document created before today

Else

’ Document created today

End If

I am using DateOnly as your code indicate that you just compare date, not the time.

If you want to compare the time as well, it is even easier:

Dim doc as NotesDocument

… set doc here…

If Cdat(doc.Created) < Cdat(“1/1/2012”) Then

’ Document created last year or earlier

Else

’ Document created this year

End if

Subject: It worked!

Thanks, The agent ran perfectly.I’ll use your tips too.