Skip to content

New DateTimePicker selects current date, but not current time #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hlship opened this issue Aug 6, 2012 · 0 comments
Open

New DateTimePicker selects current date, but not current time #402

hlship opened this issue Aug 6, 2012 · 0 comments

Comments

@hlship
Copy link

hlship commented Aug 6, 2012

When you click into a datetimepicker field that is blank, it brings up the calendar panel and displays the current date, but the time is set to "12:00 pm".

I have rather complex initialization code, as I have implemented it as a Tapestry component for selecting both dates and date/times.


    var $field = $("#" + spec.id);
    var $container = $field.parent(".input-append");
    var $hidden = $container.find("input[type=hidden]");

    var initialUTC = $hidden.val();

    var initialDate = initialUTC === "" ? null : new Date(parseInt(initialUTC));

    var picker = $field[spec.time ? "datetimepicker" : "datepicker"];

    var options = {
      dateFormat: "mm/dd/yy",
      changeMonth: spec.distant,
      changeYear: spec.distant,
      onClose: function (dateStr) {
        var date = picker.call($field, "getDate");
        $hidden.val(date == null ? "" : date.getTime());
      }
    };

    if (spec.min) {
      options.minDate = new Date(spec.min);
    }

    if (spec.max) {
      options.maxDate = new Date(spec.max);
    }


    if (spec.time) {
      _.extend(options, {
        dateTimeFormat: "mm/dd/yy hh:mm tt",
        ampm: true,
        showButtonPanel: true,
        defaultDate : new Date()
      });
    }

    picker.call($field, options);

    if (initialDate) {
      picker.call($field, "setDate", initialDate);
    }

    // If Tapestry autofocuses on a field, the widget isn't displayed. This
    // checks at page load if the field is focused, and shows the correct
    // picker.
    $(function () {
      if (document.activeElement == $field[0]) {
        $field[spec.time ? "datetimepicker" : "datepicker"]("show");
      }
    });

    $container.find(".add-on").click(function () {
      $field.focus();
    })
  }

However, I think you can pick out how options is constructed and passed to $.fn.datetimepicker(). I would expect that clicking on the field would use the defaultDate, including the time portion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant