No announcement yet.
  • Filter
  • Time
Clear All
new posts

    How to Format DateTimeItem's TextItem String?

    DateTimeItem.setUse24HourTime(Boolean.FALSE) works as expected: The Picker shows only Hours 1-12 (not 13-23) and a selector for AM|PM.

    However, the Picker selection displays in the TextItem box with 24-Hour format. E.g., I just picked "1:30 PM," why does TextItem show "13:30"? This is potentially confusing to the end user, and it doesn't match the formatting used elsewhere on the page (ListGridFields, etc).

    The most intuitive thing (to me) would be to just have the TextItem content format follow the setting for setUse24HourTime().

    Please advise, thanks.
    package com.smartgwt.sample.client;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.fields.DateTimeItem;
    public class DemoDateTimeItemFormat implements EntryPoint {
        public void onModuleLoad() {
            // Goal is to have Picker populate TextBox w/ padded format
            // "07/21/2016 02:56 PM"
            final DateTimeItem dateTimeItem = new DateTimeItem() {
                    setUse24HourTime(Boolean.FALSE); // Shows AM/PM in Picker
                    // Issue: Picker value returned to TextItem is 24-Hour format,
                    // confusing
                    // Things I tried:
                    // 1) Close, but not quite:
                    // Format is "7/21/2016, 3:15:00 PM" (not padded; don't want seconds)
                    // But this format provokes validation error "Must be a date"
                    // setDateFormatter(DateDisplayFormat.TOLOCALESTRING); // either
                    // setDisplayFormat(DateDisplayFormat.TOLOCALESTRING); // neither
                    // 2)
                    // setUseTextField(Boolean.FALSE); // Suppresses Time Display
                    // setShowPickerTimeItem(Boolean.TRUE); // Time is hidden once picked
                    // 3) No effect
                    // setTextFieldProperties(new TextItem() {
                    // {
                    // setDateFormatter(DateDisplayFormat.TOUSSHORTDATE);
                    // setTimeFormatter(TimeDisplayFormat.TOSHORTPADDEDTIME);
                    // }
                    // });
            DynamicForm dynamicForm = new DynamicForm() {
    SmartClient Version: v9.1p_2016-07-08/PowerEdition Deployment (built 2016-07-08)

    As documented, use24HourTime controls am/pm time-selection *in the DateChooser*, not the format of the displayValue in the formItem. The item will use its own dateFormatter attribute, if setDateFormatter() has been called on it, or the global default datetime format otherwise.

    So, *with your current build*, you could either call item.setDateFormatter() with a 12-hour formatter to match your 12-hour DateChooser, or you can install global 12-hour formatters using DateUtil.setShort[Date/Datetime]Formatter().

    However - as of tomorrow's builds, dated August 3 and later, we've made some changes in this area - for your use-case, where you've just changed use24HourTime in a single item, without installing 12-hour formatters, a 12-hour format will now be installed automatically for that item, as you originally expected - you can change the default format it uses by all calling item.setTimeFormatter().