How do I use the Date Picker View on mobile?

This lesson describes how to use mobilePickDate to set a date, a time or both a date and time. Screen captures and source code are provided.

Introduction

The command mobilePickDate provides a native date picker interface that allows users to set the date, the time or the date and time, depending on the parameters passed to mobilePickDate. The parameters to this command allow the selection mode, selection range, a current value and the minute interval to be specified.

Parameters are specified in seconds since the UNIX Epoch, except for the minute interval, that is specified in minutes. The selected date and time is returned in the result variable.

Getting the Date

Getting the Date

You can launch the date picker to select a date only with the following code:

on mouseUp pButtonNumber
   local tDateResult
   
   // launch the date picker
   mobilePickDate "date"
   // get the result
   put the result into tDateResult
   
   // check and display the result in the text field
   if tDateResult is 0 then 
      put "No Selection Made" into field "DateField"
   else 
      convert tDateResult from seconds to abbreviated date 
      put tDateResult into field "DateField"
   end if
end mouseUp

This code does not provide any restrictions on the date range from which a selection can be made or provide a default date value. This means that the default date is set to the date at which the date picker is launched. The screen capture shows the pick representation for date selection and the result returned to field "DateField", once the date picker is dismissed.

Getting the Time

Getting the Time

You can launch the time picker with the following code:

on mouseUp pButtonNumber
   local tDateResult
   
   // launch the time picker
   mobilePickDate "time",,,,10
   
   // get the result
   put the result into tDateResult
   
   // check and display the result in the text field
   if tDateResult is 0 then
      put "No Selection Made" into field "DateField"
   else
      convert tDateResult from seconds to short time
      put tDateResult into field "DateField"
   end if
end mouseUp

This code specifies a minute interval of ten minutes, providing a minute picker wheel with the values 00, 10, ..., 40, 50. Other settings are not provided. This means that the default time is set to the time at which the time picker is launched. The screen capture shows the pick representation for time selection and the result returned to field "DateField", once the time picker is dismissed.

Getting the Date and Time (iOS Only)

Getting the Date and Time (iOS Only)

You can launch the date and time picker by using the following code:

on mouseUp pButtonNumber
   local tSelected, tRangeStart, tRangeEnd, tInterval, tDateResult
   
   put "02/11/2018" into tSelected // the date and time selected by default
   put "01/01/2018" into tRangeStart // the minimum date and time that can be selected
   put "05/06/2018" into tRangeEnd // the maximum date and time that can be selected
   put 5 into tInterval
   
   // convert the dates into seconds since the UNIX Epoch
   convert tSelected to seconds
   convert tRangeStart to seconds
   convert tRangeEnd to seconds
   
   // launch the date and time picker
   mobilePickDate "dateTime", tSelected, tRangeStart, tRangeEnd, tInterval
   
   // get the result
   put the result into tDateResult
   
   // check and display the result in the text field
   if tDateResult is 0 then
      put "No Selection Made" into field "DateField"
   else
      convert tDateResult from seconds to internet date
      put tDateResult into field "DateField"
   end if
end mouseUp

This code restricts the date range to a value between 20 July 2011 and 8 August 2011 from which a selection can be made. The default date is set to 29 July 2011. The screen capture shows the pick representation for date selection and the result returned to field "DateField", once the date picker is dismissed.

Note: The dateTime option is only available on iOS.

10 Comments

paulo gomes

Hello,
I reproduced the example above putting the provided code in a button, but I'm getting the following error:

1. if I use the command as described in the Dictionary i get the error

button "Button-getDate": execution error at line 5 (Handler: can't find handler) near "mobilePickDate", char 1

2. if I use the code example I get the error

button "Button-getDate": execution error at line 5 (Handler: can't find handler) near "iphonePickDate", char 1

I'm using Live Code 5.5.1 for Android. Am I missing a addon or plugin ?

Thanks,
Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

no, you are not missing a plugin. The example you see in this lesson is for iOS.
If you would like to use the date picker on Android then you should use "mobilePickDate". I see you are using "mobilePickDate" and "iphonePickDate" in your comment.

"mobilePickDate" on Android also does not support as many features as are supported on iOS. This information is covered in the LiveCode dictionary. In particular, look out for the wording: (iOS Only). This indicates that a particular feature is only supported on iOS and not on Android.

Kind Regards,

Hanson

Paulo Gomes

Hi Cornelius,

Thanks for your reply.

However, I dont know were to go from here to get the date picker to work.
The Dictionary says --> "The default style on Android is "date"

My command in the mouseUp button handler is:

mobilePickDate "date"

So I dont understand why I'm getting the error since Android is supportd.

Maybe a 5.5.1 version bug and I should open a Support Ticket with RunRev?

Once more, thanks for your help.

Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

yes, the default for Android is "date", so all you have to specify is:

mobilePickDate

Kind Regards,

Hanson

paulo gomes

Hi Hanson,

Thanks for the answer. However, still does not work. I believe it does not work with Livecode, Android version. It is a simple button with mobilePickDate command in the mouseUp handler! That is the entire stack.

I opened a support ticket because if it is not supported by the Android version of Livecode, the documentation has to be changed. For the other side, following the documentation it does not work, so something has to be done. If I'm having this trouble, others may have too, right?

Well, I really appreciate your prompt answers.:)

Thanks,
Paulo

Hanson Schmidt-Cornelius

Hi Paulo,

that is odd. I tried mobilePickDate on Android and it launches the date picker for me.
I was using an Android 2.3.3 simulator with LiveCode 5.5.1.

If you launched a support ticket, please ensure you supply a sample stack with it, in case there is something wrong with the settings of the stack.

Kind Regards,

Hanson

Pablo

I do i simple project, a button and the instrucction 'mobilePickDate "date"' but always i have the error message 'button "Button": execution error at line 3 (Handler: can't find handler) near "mobilePickDate", char 1'

I need some configuration ?? If do you want i canse send you the code by mail...

thank You!!

Elanor Buchanan

Hi Pablo

The mobilePickDate command is only available on mobile, so in a simulator or on a device. This means if you try to use it on desktop, either in the IDE or in a standalone, you will get an error.

To test this example you will need to test on mobile as described in this lesson

http://lessons.livecode.com/m/2571/l/23075-how-do-i-build-an-ios-application

I hope that helps.

Elanor

Paolo

Hei!
sent a question to the support team (actually at the chat on web browser), and they sent me to this lesson telling me to write here my question. So I do that: in the dictionary it says that the result out of a choice made in mobilePickDate it is given in seconds. In my case (IOS application, latest version of Livecode indy) it is not; the result is returned in date format (i.e. 10/1/17). Is this normal behavior or is it a bug? best, Paolo

Elanor Buchanan

Hi Paolo

You are right, it is a bug. I see you have added a comment to bug 11554. I have asked the Development Team to take a look at it to confirm whether it is a documentation or functionality bug. We will update the bug report when it has been investigated.

In the meantime you might be able to use the convert command to format the date into the form you want.

Kind regards

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.