How to implement the DATE PICKER in PhoneGap/Android?

30,652

Solution 1

Why loose ur head?

A <input type="date"> will allways deppend on device's interpretation of it, in some android devices it doesn't even work,

There is plenty of plugins, addons, whatever, for it,

I personally like, and use mobiscroll: Link

Edit: Mobiscroll is now paid but there are loads of free frontend mobile frameworks and probably all of them have a datepicker, such as jQuery Mobile-datepicker.

Solution 2

What I want to happen is simple - I just want a datepicker to display when I click a certain field.

However, the same as Aleks, I don't know what to put in my html, how to use it in html, and what should I put in the html to invoke the datepicker on some input.

The documentation from the plugin is incomplete.

I found a solution from this test project. Steps are as follows:

  1. Pre-requisite: phonegap/cordova-cli installed
  2. Install Cordova's device plugin: $ cordova plugin add org.apache.cordova.device
  3. Install Dirk's datepicker plugin: $ cordova plugin add https://github.com/DURK/cordova-datepicker-plugin
  4. Copy the nativedatepicker.js from the test project and place it on your project's js folder. This file has showDatePicker(), showDateTimePicker() convenience functions.
  5. Add the ff. to index.html code:

Note: The datepicker won't show when you test it in your browser

....
    <div class="form-group">
      <label>Appointment</label>
      <input type="text" class="form-control datepicker" id="appointment">
    </div>
....
<script src="js/nativedatepicker.js"></script>
<script src="cordova.js"></script>
<script type="text/javascript">
    (function($){
        $(document).ready(function () {
            $(document).on('click', '.datepicker', function () {
                showDatePicker($(this), 'date');
            });

            $(document).on('click', '.timepicker', function () {
                showDatePicker($(this), 'time');
            });

            $(document).on('click', '.datetimepicker', function () {
                if (device.platform === "Android")
                    showDateTimePicker($(this));
                else
                    showDatePicker($(this), 'datetime');
            });
        });
    })(jQuery);
</script>

Solution 3

It seems that your currentField is undefined. Did you check the chrome console before running it on AVD ? Pls try to post the element in which you are trying to display the date as well.

For now, I am assuming that you are trying to do what the following code does

$('.nativedatepicker').focus(function(event) {
            var currentField = $(this);
            var myNewDate = new Date(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);
                var newString = newDate.toString();
                newString = newString.substring(0,15);
                currentField.val(newString);
                // 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();
            });
        });

The element is as follows

<input type="text" class="nativedatepicker" readonly value = "Fri Jun 21 2013"/>

Works like a charm !! Hope it helps !!

Solution 4

This is my working implementation. Input type is text, readonly.

$('.nativedatepicker').focus(function(event) {
        var currentField = $(this);
        var myNewDate = new Date();
        window.plugins.datePicker.show({
            date : myNewDate,
            mode : 'date',
            allowOldDates : true
        }, function(returnDate) {
            var array = returnDate.split("/");
            var day = array[2], month = array[1];
            if (day <= 9)
                day = "0" + day;
            if (month <= 9)
                month = "0" + month;
            currentField.val(array[0] + "/" + month + "/" + day);
            currentField.blur();
        });
    });

Solution 5

Why would you want to implement a custom date picker if there is an ative one available ?

You can simply use <input type="date"> to create the commonly known iOS date picker.

For more infos on input fields on mobile devices I suggest: http://blog.teamtreehouse.com/using-html5-input-types-to-enhance-the-mobile-browsing-experience

Share:
30,652
user
Author by

user

Updated on November 29, 2020

Comments

  • user
    user over 3 years

    I have tried to implement the date picker in android. I want it to get the data and show it in the text format

      <html>
      <head>
        <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="datePickerPlugin.js"></script>
      <script type="text/javascript" charset="utf-8">
    
      function dateTest() {
          var myNewDate = new Date();
    
          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();
          });
      }
    </script>
    </head>
    <body bgcolor="#ffffff">
    <hr>DatePicker Test<hr><br>
         <input type="button" onClick ="dateTest()" value ="Today's Date!!" />
         <div id="view"></div>
    </body>
    </html>
    

    I am getting it as an alert...but unable to store it as a string on the same page