当前位置: 动力学知识库 > 问答 > 编程问答 >

XPages: convert DateTime value to string using browser's locale

问题描述:

A similar question to a previous one I asked, but the difference being that this not for direct rendering from an underlying field - it's instead part of a some SSJS.

This is for a view column which displays the result of a SSJS function, which returns HTML that gets rendered. This HTML includes a date from a DateTime field, which gets converted to text using @Text. The problem I have with this is, @Text converts dates using the locale settings of the server, not the browser.

Is there an alternative to @Text(dateValue,"D0S0") that's browser locale aware?

网友答案:

The most "XPagey" way to do this is to use a date/time converter. For example (using a stand-in for the computed value):

<xp:viewColumn columnName="">
    <xp:this.value><![CDATA[#{javascript:
        new java.util.Date()
    }]]></xp:this.value>
    <xp:this.converter>
        <xp:convertDateTime type="both"/>
    </xp:this.converter>
</xp:viewColumn>

That "convertDateTime", with its built-in formats, will respect the browser's provided locale. If you set the option in the Xsp Properties to use the browser's time zone and "Round trip", it should also respect the user's time zone.

网友答案:

I've managed to get round this by using DateFormat.getDateInstance. The only problem with this is it doesn't return a short date in the same format as the XPage date converter (no leading zeros and a 2-figure year). I've got round this though with some fiddling around with the string after.

Here's the full function:

function returnLocalShortDate(ndtDate) {
    // Receives NotesDateTime object, Java date or string; returns localised date string in XPages short date format
    importPackage(java.text);
    if (@IsText(ndtDate)) { // string
        var jsDate = @TextToTime(ndtDate);
    } else if (ndtDate instanceof Date) { // Java date
        var jsDate:Date = ndtDate;
    } else if (@IsTime(ndtDate)) { // Notes date/time
        var jsDate:Date = ndtDate[0].toJavaDate();      
    } else {
        return("");
    }
    var strDate:String = java.text.DateFormat.getDateInstance(DateFormat.SHORT, context.getLocale()).format(jsDate);
    var strYear = jsDate.getFullYear();
    var strDateArray = strDate.split("/");
    strDate = ('0' + strDateArray[0]).slice(-2) + '/' + ('0' + strDateArray[1]).slice(-2) + '/' + strYear;
    return(strDate);
}
网友答案:

Actually, if you know the format you want, rather than what the user might want via their browser settings, you should use the SimpleDateFormatter class. You can supply the format in accordance with whatever pattern you want from the javadocs for that class. If you supply the NotesDocument object and the field name, this returns the date in dd-MMM-yyyy format.

function getFormattedDate ( doc:NotesDocument, fieldName:String ) {
    importPackage(java.text);

    var dateFormatter:java.text.SimpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    var d:Date = new Date(@Today());

    if ( doc.hasItem (fieldName) ) {
        var valueVector:java.util.Vector = doc.getItemValueDateTimeArray(fieldName);
        var iterator = valueVector.iterator();

        while (iterator.hasNext()) {
            var itemvalue = iterator.next();
            if ((typeof(itemvalue)).endsWith("DateTime")) {
                d = new Date(itemvalue.toJavaDate());
                return dateFormatter.format(d);
            }
        }
    } else {
        return fieldName + " is not on the document"
    }
}

I owe credit to Declan Lynch's blog entry on date formatting, which takes a little debugging because SSJS returns the date value as an Vector now.

分享给朋友:
您可能感兴趣的文章:
随机阅读: