Search This Blog

Sunday, May 24, 2015

How to save date in a document item without time part on back-end

Hi

This is not something really tricky but it's nice to know.

Let's imagine you need to save a single date or an array of dates without time part in a NotesDocument item. F.x. you need to do this from an agent so you don't have a form opened in UI with a date-field on it which property "Display time" is disable.

You could write something like

Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim singleDate As Variant
Dim arrayOfDates(1) As Variant

Set db = s.currentdatabase
Set doc = db.createdocument

singleDate = DateNumber(2015, 5, 22)

arrayOfDates(0) = DateNumber(2015, 5, 12)
arrayOfDates(1) = DateNumber(2015, 6, 8)

Call doc.replaceitemvalue("singleDate", arrayOfDates)
Call doc.replaceitemvalue("arrayOfDates", arrayOfDates)
Call doc.save(True, False)

However if you check the item value you will see that there is a time part near each date value




So, I found following options to achieve the goal:

1) Use objects of NotesDateTime class
2) Use @Formula

Example with objects of NotesDateTime class

Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim arrayOfDates(1) As Variant
Dim ndt As New NotesDateTime("3.3.2015")
Dim ndt2 As New NotesDateTime("4.4.2015")

Set db = s.currentdatabase
Set doc = db.createdocument

Set arrayOfDates(0) = ndt
Set arrayOfDates(1) = ndt2

Call doc.replaceitemvalue("singleDate", ndt)
Call doc.replaceitemvalue("arrayOfDates", arrayOfDates)

Call doc.save(True, False)

Example with @Formula

Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim singleDate As variant
Dim arrayOfDates(1) As Variant
Dim strJoinedArray As string
Dim ret As variant

Set db = s.currentdatabase
Set doc = db.createdocument

singleDate = DateNumber(2015, 5, 22)

arrayOfDates(0) = DateNumber(2015, 5, 12)
arrayOfDates(1) = DateNumber(2015, 6, 8)

ret = Evaluate({@SetField("singleDate";[} & singleDate & {])}, doc)

ForAll x In arrayOfDates
If strJoinedArray <> "" Then strJoinedArray = strJoinedArray & ":"
strJoinedArray = strJoinedArray & {[} & x & {]} 
End ForAll
ret = Evaluate({@SetField("arrayOfDates";} & strJoinedArray & {)}, doc)

Call doc.save(True, False)



1 comment:

  1. Have a look at the SetAnyTime() method on the NotesDateTime class. I'm sure this does what you want. Rob

    ReplyDelete