This is a simple date picker. I am using 3 spinners for date format dd MMM yyyy. I was thinking to add a simple date picker in my apps.
Version 1.0.1:
1. added Leap Year check
2. added SetCurrentDate sub
3. added Min and Max Year
Version 1.0.1:
1. added Leap Year check
2. added SetCurrentDate sub
3. added Min and Max Year
B4X:
#Region Project Attributes
#ApplicationLabel: Simple Date Spinner
#VersionCode: 2
#VersionName: 1.0.1
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
End Sub
Sub Globals
Dim spnDD As Spinner
Dim spnMM As Spinner
Dim spnYY As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim MinYear As Int
Dim MaxYear As Int
Dim i As Int
Dim d As Long
Activity.LoadLayout("Sample")
' Initialize Year spinner
MinYear = 2000
MaxYear = 2016
For i = MinYear To MaxYear
spnYY.Add(i)
Next
' Initialize Month spinner
For i = 1 To 12
DateTime.DateFormat = "d/M/yyyy"
d = DateTime.DateParse("1/" & i & "/2016")
DateTime.DateFormat = "MMM"
spnMM.Add(DateTime.Date(d))
Next
' Initialize Day spinner
For i = 1 To 31
If i < 10 Then
spnDD.Add("0" & i)
Else
spnDD.Add(i)
End If
Next
SetCurrentDate
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub spnMM_ItemClick (Position As Int, Value As Object)
PopulateDD
End Sub
Sub spnYY_ItemClick (Position As Int, Value As Object)
PopulateDD
End Sub
Sub PopulateDD
Select Case (spnMM.SelectedIndex + 1)
Case 1, 3, 5, 7, 8, 10, 12
If spnDD.Size = 28 Then
spnDD.Add("29")
End If
If spnDD.Size = 29 Then
spnDD.Add("30")
End If
If spnDD.Size = 30 Then
spnDD.Add("31")
End If
Case 4, 6, 9, 11
If spnDD.Size = 28 Then
spnDD.Add("29")
End If
If spnDD.Size = 29 Then
spnDD.Add("30")
End If
If spnDD.Size = 31 Then
spnDD.RemoveAt(30)
End If
Case Else ' 2
If spnDD.Size = 31 Then
spnDD.RemoveAt(30)
End If
If spnDD.Size = 30 Then
spnDD.RemoveAt(29)
End If
If LeapYear(spnYY.SelectedItem) Then ' Leap Year
If spnDD.Size = 28 Then
spnDD.Add("29")
End If
Else
If spnDD.Size = 29 Then
spnDD.RemoveAt(28)
End If
End If
End Select
End Sub
' Credit to: Jost aus Soest
' URL: https://www.b4x.com/android/forum/threads/islapyear.16107/#post-91523
Sub LeapYear(Year As Int) As Boolean
If Year Mod 400 = 0 Then Return True
If Year Mod 100 = 0 Then Return False
If Year Mod 4 = 0 Then Return True
Return False
End Sub
Sub SetCurrentDate()
spnYY.SelectedIndex = spnYY.IndexOf(DateTime.GetYear(DateTime.Now))
spnMM.SelectedIndex = DateTime.GetMonth(DateTime.Now) - 1
spnMM_ItemClick(DateTime.GetMonth(DateTime.Now) - 1, DateTime.GetMonth(DateTime.Now))
spnDD.SelectedIndex = DateTime.GetDayOfMonth(DateTime.Now) - 1
End Sub
Attachments
Last edited: