mrfoh / cordova-datepicker-plugin

Datepicker for Apache Cordova
Apache License 2.0
13 stars 4 forks source link

Datepicker for Apache Cordova 3.0.0

Run the following command (Check that you have node.js and cordova installed)

cordova plugin add https://github.com/mrfoh/cordova-datepicker-plugin

  1. Add a class="nativedatepicker" to your element for which you want the native datepicker
  2. Add the following jQuery fragment to handle the click on these input elements:
    $('.nativedatepicker').focus(function(event) {
        var currentField = $(this);
        var myNewDate = Date.parse(currentField.val()) || new Date();

        // Same handling for iPhone and Android
        window.plugins.datePicker.show({
            date : myNewDate,
            mode : 'date', // date or time or blank for both
            allowOldDates : true
        }, function(returnDate) {
            var newDate = new Date(returnDate);
            currentField.val(newDate.toString("dd/MMM/yyyy"));

            // This fixes the problem you mention at the bottom of this script with it not working a second/third time around, because it is in focus.
            currentField.blur();
        });
    });

    $('.nativetimepicker').focus(function(event) {
        var currentField = $(this);
        var time = currentField.val();
        var myNewTime = new Date();

        myNewTime.setHours(time.substr(0, 2));
        myNewTime.setMinutes(time.substr(3, 2));

        // Same handling for iPhone and Android
        plugins.datePicker.show({
            date : myNewTime,
            mode : 'time', // date or time or blank for both
            allowOldDates : true
        }, function(returnDate) {
          // returnDate is generated by .toLocaleString() in Java so it will be relative to the current time zone
            var newDate = new Date(returnDate);
            currentField.val(newDate.toString("HH:mm"));

            currentField.blur();
        });
    });
  1. It is recommended to prefil these input fields and make these fields read only with a standard date format, preferably with three letter month so it can always be parsed.

  2. You may need to convert the result of date.parse() back to an object to get the picker to work a second or third time around. If so, try inserting this code after the myNewDate declaration:

        myNewDate = new Date (myNewDate);
    }