Announcement

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

    DateTimeItem - How to format the date/time

    SmartGWT Power 2.3 / Internet Explorer

    What is the correct way to ensure that the date value is formatted properly in the DateTimeItem? My attempt to do so below, did not work.

    Code:
    final DateTimeItem uiStartDateTimeField = new DateTimeItem("start", "Start Time");
    uiStartDateTimeField.setDisplayFormat(DateDisplayFormat.TOUSSHORTDATETIME);
    The restful data source is sending its data "MM/DD/YYYY HH:MM". I tried using the following:

    Code:
    DateUtil.setNormalDateDisplayFormat(DateDisplayFormat.TOUSSHORTDATETIME);
    at the start of my "onModuleLoad()" method, but it did not propogate to the DateTimeItem.

    Thanks for reviewing this.
    Last edited by acole; 17 Jan 2011, 16:49.

    #2
    new SimpleDateFormat("MM-DD-YYYY").format(new Date())

    Comment


      #3
      The following logic appears to work.

      Code:
      final DateTimeItem uiStartDateTimeField = new DateTimeItem("start", "Start Time");
              uiStartDateTimeField.setDisplayFormat(DateDisplayFormat.TOUSSHORTDATETIME);
              uiStartDateTimeField.addChangedHandler(new ChangedHandler()
              {
                  public void onChanged(ChangedEvent event)
                  {
                      Date valueDate = (Date) uiStartDateTimeField.getValue();
                      DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MM/dd/yyyy hh:mm");
                      uiStartDateTimeField.setValue(dateFormatter.format(valueDate));
                  }
              });

      Comment


        #4
        While the previous logic enabled me to correct the text value, the DynamicForm.validate() marks the field as an invalid date and refreshes it to the format of "Sunday, December 26, 2010 12:00:00 PM". This is not the format of "12/26/2010 12:00" that I need.

        I am sure that I am missing something here. I will go back to my original question:

        How do I ensure that a date/time value is formatted properly in the DateTimeItem?

        Comment


          #5
          I am still looking for some advice on how to properly handle date/time formatting. The DynamicForm validation logic is blocking me at this time.

          Comment


            #6
            To apply a custom format, use the formatter APIs, not setValue() on change - the fact that you're using setValue() to force the value to a String when the value of a DateItem should always be a Date is what's creating the validation issue.

            If you want a custom format (not one of the built-ins) just for this item, use FormItem.setEditorValueFormatter. Also add a setEditorValueParser if you find the value isn't being parsed after using DateItem.setInputFormat().

            Comment

            Working...
            X