I know similar questions got asked before but none of the answers there could help me, so maybe I can find the right answer here. I'm writing a script which is supposed to take a NotesDateTime type and convert it to only include the date. I know that this should be possible with the ".DateOnly" extension. However I can't seem to get it to work.
Here's the relevant Code in the agent:
Dim datevarstr As String
Dim datevar As NotesDateTime
Set datevar = doc.Datum(0).DateOnly
datevarstr = Cstr(datevar)
path_var = Cstr(datevarstr)
On the following line there's an error I can't seem to fix properly:
datevarstr = Cstr(datevar)
It say's "Type mismatch on: DATEVAR"
I almost certain there's an easy solution but I'm new to programming, especially in LotusScript, so I can't seem to find it.
Thanks in advance.
Your variable "datevar" is an object of class "NotesDateTime". Objects are no primitives and cannot be converted to a string easily.
In addition "DateOnly" is a property of that class NotesDateTime and
doc.GetItemValue( "Datum" )(0)
which is a long form for
doc.Datum(0)
does not return a NotesDateTime but a variant of type Date/Time what is a big difference.
That means: After that line the variable datevar is not even initialized but "Nothing". And that is the reason for the type mismatch.
You need to do the following:
The resulting code:
Set datevar = New NotesDateTime( doc.Datum(0) )
datevarstr = dateVar.DateOnly
path_var = datevarstr
Now to the big downside of this:
You can never be sure that the format of your string is correct.
As you use the German item name "Datum" I assume that you might have stumbled over the different date formats (dd.mm.yyyy vs. mm/dd/yyyy vs. yyyy-mm-dd).
If your code runs on a server with English locale / date settings then the result for today in "path_var" will be 01/25/2023. If you run it on your client with German locale it will most probably be 25.01.2023, if you change your os setting to English you will again get the English format: not very good and predictable option for directories...
Better use the "Format" option to directly format your date however you need it to be: easier, less code and more consistent:
'- ISO date, best for sorting directories
datevarString = Format( doc.Datum(0), "yyyy-mm-dd" )
'- German
datevarString = Format( doc.Datum(0), "dd.mm.yyyy" )
'- English
datevarString = Format( doc.Datum(0), "mm/dd/yyyy" )