Android Question i am newbie confused again

67biscuits

Member
So, have a quick butchers at the code here...

quirky:
Sub createGrid(Col As Int, Row As Int)
    Dim now As Long = DateTime.Now
    Dim pnl As Panel
    Dim spnM As Spinner
    Dim spnY As Spinner
    Dim gap As Int = 2dip
    Dim wday As Label
    pnl.Initialize("pnl")
    spnM.initialize("spnM")
    spnY.Initialize("spnY")
    wday.Initialize("wday")

    pnl.Color = xui.Color_white
    pnl.Elevation = 20dip
    pnlHours.AddView(pnl,40dip, 0dip, 280dip - gap, 380dip - gap)
    Dim spnWidth As Int = pnl.Width / 2 - gap
    Dim btnWH As Int = pnl.Width/Col
    Dim btns As List
    btns.Initialize
    Dim days As List = Array("Su","Mo","Tu","We","Th","Fr","Sa")
'========ad the views ==============
    pnl.AddView(spnM, 0dip, 0dip, spnWidth, 40dip)
    pnl.AddView(spnY, spnWidth- gap , 0dip, spnWidth - gap , 40dip)
    Dim d As Int = 0
    Dim top As Int = 40dip
    For r = 1 To Row
        top = top + 40dip
        For c = 0 To Col - 1
            d = d + 1
            Dim btn As Button
            btn.Initialize("btn")
            pnl.AddView(btn, c*40dip, top + gap, btnWH, btnWH)
            btn.TextSize = 10
            btns.Add(btn)
        Next
    Next
    Dim aMonth As String = activeMonth
    If activeMonth < 10 Then aMonth = $"0${aMonth}"$
Log($"aMonth = ${aMonth}"$)

    Dim TT As String = $"${aMonth}/01/${activeYear}"$
    Dim firstday As Int

DateTime.DateFormat="F"

Log($"TT: ${TT}"$)
    firstday = DateTime.GetDayOfWeek(DateTime.DateParse(TT))
    Log(firstday)

    btns.Get(firstday).As(Button).Text = "F"
'=======fill the spots =============== 
    spnM.AddAll(Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
    spnY.Addall(Array As Int( 2024,2025))
    spnM.SelectedIndex = (DateTime.getMonth(now)-1)
    spnY.selectedindex = (DateTime.GetYear(now))

    For x = 0 To 6
        Dim wday As Label
        wday.Initialize("wday")
        pnl.AddView(wday, x * btnWH + gap, 40dip + gap, btnWH, btnWH)
        wday.Text = $" ${days.get(x)}"$
    Next

    For x = firstday To DateUtils.NumberOfDaysInMonth(activeMonth,activeYear) + firstday
        btns.Get(x).As (Button).text = (x-firstday)+1
    Next
      
End Sub

on lines 44 to 48 (approx) you see a datetime.dateformat = "F". If I take that out, ignoring the Log(), the next line fails always! Why is that? I wonder
Also, same line, why is firstday ALWAYS 2, regardless of the month quiried?
What newby's blunder am I committing??? Help me please by pointing out that one small tick, comma, keystroke I have missed.

Thanks folks
 

klaus

Expert
Licensed User
Longtime User
What is the DateFormat before line 44 ?
Is it really "MM/dd/yyyy" ?

Then, in Line 44 you change the DateFormat ="F", which means Day of week in month. Do you really want this format ?
Then, line 47: firstday = DateTime.GetDayOfWeek(DateTime.DateParse(TT))
The DataFormat is wrong because now it is "F" and no more "MM/dd/yyyy".
 
Upvote 0

67biscuits

Member
Where does this date format come from? Never seen "F".

What is the value of TT?
1745247662125.png

The value of TT is the first day of a month, formatted MM/dd/yyyy, eg: 04/01/2025 in order to capture format "F", the first weekday of the month. (Int 1 to 7). That then would give me the first day of the week to start inserting the days, 1 through 30 etc
 
Upvote 0

67biscuits

Member
What is the DateFormat before line 44 ?
Is it really "MM/dd/yyyy" ?

Then, in Line 44 you change the DateFormat ="F", which means Day of week in month. Do you really want this format ?
Then, line 47: firstday = DateTime.GetDayOfWeek(DateTime.DateParse(TT))
The DataFormat is wrong because now it is "F" and no more "MM/dd/yyyy".
The format prior to "F" is "E, dd MMMM" set globally for display purposes. I initially thought that by changing to "F" I might be able able to extract the integer I need in order to find the first day of the month, but then realized I need something there in order for the date to even parse the date. I am still experimenting to determine which dateformat does what to the outcome. For example, if I set the format to "MM", it changes the desired outcome, day of the week to Thursday, 5. all other formats the out come is Monday 2
I isolated the Log statements for the outcome as an experiment, so I could force the date, eg
Log(DateUtils.GetDayOfWeekName(DateTime.DateParse("04/01/2024")))
Log(DateTime.GetDayOfWeek(DateTime.DateParse("04/01/2025")))
And this, regardless of what I set the date to, will spit out Monday and 2, or Thursday and 5 (If I set the format to "MM"
I have now made so many adjustments, I am befuddled. If I remove the format statement, the program crashes and halts at that parse line (47)

Yes, I want day of week in month, though the term does seem a little ambiguous. What I want is the number of the day in the week, 1 through 7. Maybe "u" is the choice I need? I tried "u", but the outcome is still the same.
If I change the date format to "MM/dd/yyyy" it does not affect the outcome. I still get Monday and 2

The current state of the sub is very different now, as I have been trying to get this to work

last and current rendition:
Sub createGrid(Col As Int, Row As Int)
    Dim now As Long = DateTime.Now
    Dim pnl As Panel
    Dim gap As Int = 2dip
    Dim wday As Label
    Public spn As Spinner
   
    pnl.Initialize("pnl")
    wday.Initialize("wday")

    pnl.Color = xui.Color_white
    pnl.Elevation = 20dip
    pnlHours.AddView(pnl,40dip, 0dip, 280dip - gap, 380dip - gap)
    Dim spnWidth As Int = pnl.Width / 2 - gap
    Dim btnWH As Int = pnl.Width/Col
    Dim btns As List
    btns.Initialize
    Dim days As List = Array("Su","Mo","Tu","We","Th","Fr","Sa")
    Dim mths As List = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    '========add the views ==============

    'create and set Month selector spinner  ==================
    spn.initialize("spn")
    pnl.AddView(spn, 0dip, 0dip, spnWidth, 40dip)
    For x = 0 To DateTime.GetMonth(now)-1
        spn.Add(mths.get(x))
    Next
    spn.SelectedIndex = (activeMonth-1)
   
    'create and set year selector spinner  =============
    spn.initialize("spn")
    pnl.AddView(spn, spnWidth- gap , 0dip, spnWidth - gap , 40dip)
    spn.Addall(Array As Int( DateTime.GetYear(now)- 1, DateTime.GetYear(now)))
    spn.selectedindex = (DateTime.GetYear(now))
   
'create and set the 42 possible month days buttons
    Dim d As Int = 0
    Dim top As Int = 40dip
    For r = 1 To Row
        top = top + 40dip
        For c = 0 To Col - 1
            d = d + 1
            Dim btn As Button
            btn.Initialize("btn")
            pnl.AddView(btn, c*40dip, top + gap, btnWH, btnWH)
            btn.TextSize = 10
            btns.Add(btn)
            btn.Text = ""
        Next
    Next
    Dim aMonth As String = activeMonth
    If aMonth.Length = 1 Then
        aMonth = $"0${aMonth}"$
    End If
   

    DateTime.DateFormat = "MM/dd/YYYY"
    Dim TT As String = $"${aMonth}/01/${activeYear}"$
   
    Dim firstI As Int
    Dim firstL As Long
    firstL = DateTime.DateParse(TT)
    firstI = DateTime.GetDayOfWeek(firstL)
    Log($"TT ${TT}"$)
    Log($"firstL ${firstL}"$)
    Log($"Weekname ${DateUtils.GetDayOfWeekName(firstL)}"$)

'=======fill the spots ===============  

    For x = 0 To 6
        Dim wday As Label
        wday.Initialize("wday")
        pnl.AddView(wday, x * btnWH + gap, 40dip + gap, btnWH, btnWH)
        wday.Text = $" ${days.get(x)}"$
    Next

    For x = firstI To (DateUtils.NumberOfDaysInMonth(activeMonth,activeYear) + firstI)-1
        btns.Get(x).As (Button).text = (x-firstI)+1
    Next

    If activeMonth = DateTime.GetMonth(now) Then
        btns.Get(DateTime.GetDayOfMonth(now)+1).As(Button).Color = xui.Color_Red
    End If
    getFirstDay
End Sub
 
Upvote 0

67biscuits

Member
I've made a few adjustments, renamed firstdayI to day1, added a last1 variabl;e to capture the last day of the month, compressed a few lines and reformatted the date to MM/dd/yyyy, and it seems to work now. Honestly I am not sure why. I will go through the previous code and current code and pick it apart very carefully to see the difference.
But it works. Thanks folks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Be aware that the DateFormat = "F" is Day of week is month.
This means the index of the day in the month.
Example:
Today 04/22/2025
The day in the week = 3; Tuesday
DateFormat = "F" returns 4, because the 22nd is the fourth Tuesday in mai this year.
 
Upvote 0

67biscuits

Member
Be aware that the DateFormat = "F" is Day of week is month.
This means the index of the day in the month.
Example:
Today 04/22/2025
The day in the week = 3; Tuesday
DateFormat = "F" returns 4, because the 22nd is the fourth Tuesday in mai this year.
of course, the nth occurrence of a day. Gotcha!!
Is there an optimal format for parsing a date? I seem to be stumbling on that one a lot.
In the globals the dateFormat is set to "E dd MMM" eg "Tue 22 Apr" for easy readability. I can swap the date format when required and replace the original going forward.
I've made a datepicker that constructs the date using two variables, activeMonth and activeYear, to determine where to start the calendar I insert "01/" in TT (MM/dd/yyyy). Is that the best format in order to parse a string date to a long date?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I mostly use this format: "yyyy.MM.dd", it is the easiest for sorting.
But i use also often Ticks mainly for saving dates.
Then, whatever you want to do you can change the format to your needs, but do not forget go back to the previous one.
In the ClsWheels Class i use it too internally, because the first criteria is the year to know about February, then next the month for the number of days.
In the class i memorize the current format of the user, change it internally and send it back in the current user format.
 
Last edited:
Upvote 0

67biscuits

Member
I mostly use this format: "yyyy.MM.dd", it is the easiest for sorting.
But i use also often Ticks mainly for saving dates.
Then, whatever you want to do you can change the format to your needs, but do not forget go back to the previous one.
In the ClsWheels Class i use it too internally, because the first criteria is the year to know about February, then next the month for the number of days.
In the class i memorize the current format of the user, change it internally and send it back in the current user format.
I also save everything with ticks. At least I do try to. Occasionally I let one slip and then I have to build in a routine to convert strings to long. But it's all just about learning the language anyway, so it's all beneficial in the end.
you help is appreciated, as is your insight and experience.
Cheers

charley
 
Upvote 0
Top