Date Picker Wheel only shows the past date

I got this code from https://github.com/mdsandell/DatePicker/blob/master/datePickerWheel.lua where it will returns a date picker wheel and gets user to choose any date from today’s date to the past 100 years.

It is a great code because it counts for the leap year too. I intend to use it in my application but I can’t get to adjust it to supports my requirements =>> to display the past and future years.

Help me please  :slight_smile:

--[[The MIT License (MIT) Copyright (c) 2015 Mark Sandell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.]] local widget = require("widget") -- Create tables to hold data for months, days, and years. local dayCount = { 31, -- January 28, -- February 31, -- March 30, -- April 31, -- May 30, -- June 31, -- July 31, -- August 30, -- September 31, -- October 30, -- November 31, -- December } -- Figure out the local month names. local months = {} local now = {year=2015, month=1, day=1, isdst=false} for i=1, 12 do now.month = i months[i] = os.date("%B", os.time(now)) end -- Add the last 100 years. local years = {} local currYear = tonumber(os.date("%Y")) + 1 for i = 100, 1, -1 do years[i] = currYear - i end local function isLeapYear(year) --[[:Parameters: year : int Year :Returns: If the given year is a leap year :Rtype: boolean]] if 0 == year % 4 then if 0 == year % 100 then if 0 == year % 400 then return true else return false end else return true end else return false end end local function daysInMonth(month, year) --[[:Parameters: month : int Number of month year : int Year, used to calculate leap years :Returns Number of days in the given month for the given year :Rtype: int]] if year and month == 2 and isLeapYear(year) then return 29 end return dayCount[month] end local function daysTable(month, year) --[[:Parameters: month : int Number of month year : int Year, used to calculate leap years :Returns: A table of day numbers based on the given month and year. (e.g. {1,2,3,...,31})]] local t = {} for i=1, daysInMonth(month, year) do t[#t+1] = i end return t end local function newWheel(year, month, day) --[[Does not support years in the future. Only goes back 100 years. These should be easy to adjust if needed. :Parameters: year : int Selected year month :int Selected month (1-12) day : int Selected day]] local columnData = { { -- Months align = "right", width = 140, startIndex = month, labels = months }, { -- Days align = "center", width = 60, startIndex = day, labels = daysTable(month, year) }, { -- Years align = "center", width = 80, startIndex = currYear - year, labels = years } } return widget.newPickerWheel{columns = columnData} end function widget.newDatePickerWheel(year, month, day) year = year or tonumber(os.date("%Y")) month = month or tonumber(os.date("%m")) day = day or tonumber(os.date("%d")) local w = display.newGroup() w.wheel = newWheel(year, month, day) w:insert(w.wheel) function w:getValues() return self.wheel:getValues() end -- NOTE: If day or year column are still scrolling when month column changes, those will -- snap back to their original selection. function w:monitor() -- Get selections from the picker wheel. local values = self:getValues() -- CORONA BUG: Sometimes the values can be nil. -- This happens when one of the tables stopped scrolling but hasn't "snapped" to a selected index. if not values[1] or not values[2] or not values[3] then return end local month = values[1].index local year = tonumber(values[3].value) local maxDays = daysInMonth(month, year) -- If the selected month has changed and the month has a different number of days than -- before, or the selected month is February and the year changes to/from a leap year, -- then redraw the picker wheel. if (month ~= self.selectedMonth and maxDays ~= daysInMonth(self.selectedMonth, year)) or (month == 2 and year ~= self.selectedYear and isLeapYear(year) ~= isLeapYear(self.selectedYear)) then -- Make sure we no longer have a day selected greater than the number of days in -- the current month. local day = math.min(values[2].index, maxDays) -- Remove the old wheel. self.wheel:removeSelf() -- Create the new wheel. self.wheel = newWheel(year, month, day) self:insert(self.wheel) -- Save the current selection so we can tell if it changes. self.selectedMonth = month self.selectedYear = year end end function w:finalize(event) timer.cancel(self.timer) self.timer = nil self.wheel:removeSelf() self.wheel = nil end w:addEventListener("finalize") -- Monitor for changes roughly 30 times per second. w.timer = timer.performWithDelay(33, function() w:monitor() end, -1) return w end

You’ll get more help if you make a sample project with that code and zip it up, then attach the zip file here.

This is the block of code that generates the year labels:

-- Add the last 100 years. local years = {} local currYear = tonumber(os.date("%Y")) + 1 for i = 100, 1, -1 do years[i] = currYear - i end 

Study that code and see if you can figure out how to change the values being inserted in into the pickerWheel.

Rob

yes I have already put some adjustment to the code but it still shows the same…

 

i add current year function to the code,

 

– Add the present 100 years.

local years1 = {}

for i = 1, 100, 1 do

years1[i] = currYear1 + i

end

and then 

local function newWheel(year, month, day) --[[Does not support years in the future. Only goes back 100 years. These should be easy to adjust if needed. :Parameters: year : int Selected year month :int Selected month (1-12) day : int Selected day]] local columnData = { { -- Months align = "right", width = 140, startIndex = month, labels = months }, { -- Days align = "center", width = 60, startIndex = day, labels = daysTable(month, year) }, { -- Years align = "center", width = 80, startIndex = currYear1 + year1, --add new current year here labels = years } } return widget.newPickerWheel{columns = columnData} end

but still shows the same on the date picker wheel. The function also says don’t support for years in the  future. i dont know how to adjust the code.

Did you change:

labels = years

to

labels = years1

??

i did and it displays runtime error

ERROR: Runtime error

02:21:36.201  ?:0: attempt to index field ‘?’ (a nil value)

zip up your project and share it here… let someone look at the actual project.  

in this project, I want to ask the user to select a date from future years and retrieve the date to another screen.
 

The retrieving part works well but I cant adjust the date picker wheel to display future years.

I already change the part to add 100 years in the future and the labels but the system returns  error.

Please help.

Thank you

No. That isn’t what I asked you to upload.   Upload a runable project. I don’t have time to write a project for you.

Include main.lua, config.lua, build.settings, and any support file you need.

This sample should VERY short and simple, just including the code needed to demonstrate your issue, but it must run. 

PS - It can run and produce error message, but I want to drop main.lua on the simulator, then jump into debug.

**UPDATED**
 
I got tired of waiting and did the following:

  1. Downloaded your zip file.
  2. Downloaded original project.
  3. Created folder with this content:
  • 1_DatePicker-master - Original code you mentioned in first post.
  • 2_help me - Your code.  Not sure how you run this.
  • 3_example - A copy of ‘1_DatePicker-master’ that has been modified to show past and future years.

Code is attached below.
 
I only show the past 10 and future 10 years.
 
I edited datePickerWheel.lua and changed this:

-- Add the last 100 years. local years = {} local currYear = tonumber(os.date("%Y")) + 1 for i = 100, 1, -1 do years[i] = currYear - i end

to this:

-- Add the past 10 years and the next 10 years. 100 years is going to make the control very bloated. local yearsBeforeAfter = 10 local years = {} local currYear = tonumber(os.date("%Y")) + 1 + yearsBeforeAfter for i = (2\*yearsBeforeAfter), 1, -1 do years[i] = currYear - i end

I then edited example.lua

changing this:

 if time \> os.time() then native.showAlert("Date Picker", "You cannot choose a date in the future.", {"OK"}) return false end

to this:

 --[[if time \> os.time() then native.showAlert("Date Picker", "You cannot choose a date in the future.", {"OK"}) return false end --]]

If you downloaded this in the last few minutes, get it again.  I just changed example.lua too.

sorry for the inconvenience… hope this one is right.

just tap the black rectangle, it is supposed to listen and display the date picker wheel but now it only displays error

I downloaded you file. Let me try it

Thank you so much, roaminggamer !!! and rob miracle too

it worked welll ! Thank god! Thank you so much for helping

You’ll get more help if you make a sample project with that code and zip it up, then attach the zip file here.

This is the block of code that generates the year labels:

-- Add the last 100 years. local years = {} local currYear = tonumber(os.date("%Y")) + 1 for i = 100, 1, -1 do years[i] = currYear - i end 

Study that code and see if you can figure out how to change the values being inserted in into the pickerWheel.

Rob

yes I have already put some adjustment to the code but it still shows the same…

 

i add current year function to the code,

 

– Add the present 100 years.

local years1 = {}

for i = 1, 100, 1 do

years1[i] = currYear1 + i

end

and then 

local function newWheel(year, month, day) --[[Does not support years in the future. Only goes back 100 years. These should be easy to adjust if needed. :Parameters: year : int Selected year month :int Selected month (1-12) day : int Selected day]] local columnData = { { -- Months align = "right", width = 140, startIndex = month, labels = months }, { -- Days align = "center", width = 60, startIndex = day, labels = daysTable(month, year) }, { -- Years align = "center", width = 80, startIndex = currYear1 + year1, --add new current year here labels = years } } return widget.newPickerWheel{columns = columnData} end

but still shows the same on the date picker wheel. The function also says don’t support for years in the  future. i dont know how to adjust the code.

Did you change:

labels = years

to

labels = years1

??

i did and it displays runtime error

ERROR: Runtime error

02:21:36.201  ?:0: attempt to index field ‘?’ (a nil value)

zip up your project and share it here… let someone look at the actual project.