I'm using an Edit Box control to display a date field. When the XPage is saved, I would like to save the date only (now both date and time are being saved). Is there any way of doing this?
Here is my code:
<xp:inputText id="dateReparatur" value="#{document1.dateReparatur}">
<xp:this.converter>
<xp:convertDateTime type="date" dateStyle="long">
</xp:convertDateTime>
</xp:this.converter>
<xp:dateTimeHelper></xp:dateTimeHelper>
</xp:inputText></xp:td>
UPDATE: I have now implemented the following code:
var dt = currentDocument.getItemValueDateTime("dateReparatur");
var dateonly = dt.getDateOnly();
currentDocument.replaceItemValue("dateReparatur",dateonly);
This gives me the date only, however in Notes the field type is now text rather than Date/Time, which is what I was hoping for.
This code worked for me:
<xp:this.postSaveDocument><![CDATA[#{javascript:
var dt:DateTime = document1.getItemValueDateTime("dateReparatur");
dt.setAnyTime();
currentDocument.getDocument(true).replaceItemValue("dateReparatur", dt);
currentDocument.getDocument(true).save()
}]]></xp:this.postSaveDocument>
It does work at postSaveDocument
event only. If you put the same code into querySaveDocument
event (without document save() line of course) the date field gets polluted with time after event during saving.
An alternative is to execute computeWithForm
at querySaveDocument
event:
<xp:this.querySaveDocument><![CDATA[#{javascript:
document1.getDocument(true).computeWithForm(true, true)
}]]></xp:this.querySaveDocument>
You'd have to add an Input Translation
formula to date field(s) in your form:
@Date(@ThisValue)
computeWithForm
has a poor performance and causes sometimes side effects on field values though but might be a good solution especially if you have a lot of such date-only-fields.
getDateOnly()
returns a string. Try this:
dt.setAnyTime();
currentDocument.replaceItemValue("dateReparatur", dt);
Or you may have to get the Document
:
currentDocument.getDocument(true).replaceItemValue("dateReparatur", dt);
来源:https://stackoverflow.com/questions/19636641/xpages-save-date-only-in-date-field