Search code examples
xpagesxpages-ssjs

preset default date and time on an xpage


I'm trying to set a default date and time on a inputfield on a xpage. So far I can only set the date :

<xp:inputText id="enddate" value="#{viewScope.einddatum}">

    <xp:this.defaultValue><![CDATA[#{javascript:test ="2023-05-03";

    return test ;

}]]></xp:this.defaultValue>
    <xp:dateTimeHelper id="dateTimeHelper1"></xp:dateTimeHelper>
    <xp:this.converter>
        <xp:convertDateTime type="both" dateStyle="short">
        </xp:convertDateTime>
    </xp:this.converter>
</xp:inputText>

With this string, the date is correctly selected and the time defaults to 00:00:00

When I try to add time to my test string , the selected date stops working ...

How can I pre select both date and time ?


Solution

  • You can return a Java Date object with the desired date and time. This example will set the date and time to the current date and time:

    <xp:inputText id="enddate" value="#{viewScope.einddatum}">
        <xp:this.defaultValue><![CDATA[#{javascript:
            return new java.util.Date();    
        }]]></xp:this.defaultValue>
        <xp:dateTimeHelper id="dateTimeHelper1"></xp:dateTimeHelper>
        <xp:this.converter>
            <xp:convertDateTime type="both" dateStyle="short">
            </xp:convertDateTime>
        </xp:this.converter>
    </xp:inputText>
    

    Similarly, this will set it to one week from now at 8.00 (using one way of many for instantiating a Java date object):

    <xp:inputText id="enddate" value="#{viewScope.einddatum}">
        <xp:this.defaultValue><![CDATA[#{javascript:
            var dateTimeVariable = java.time.LocalDate.now().plusDays(7).atTime(8, 0);
            return java.util.Date.from(dateTimeVariable.atZone(java.time.ZoneId.systemDefault()).toInstant());  
        }]]></xp:this.defaultValue>
        <xp:dateTimeHelper id="dateTimeHelper1"></xp:dateTimeHelper>
        <xp:this.converter>
            <xp:convertDateTime type="both" dateStyle="short">
            </xp:convertDateTime>
        </xp:this.converter>
    </xp:inputText>