Android Question DateTime.GetMonth always returns 1

SeaBee

Member
Licensed User
I am probably being exceptionally obtuse, but when I take valid date string from a text box, parse it into a ticks long, and then query the long with DateTime.GetMonth, it returns 1, regardless of the actual month. The year and Day-of-month are returned correctly. Also, if I use DateTime.Parse on DateTime.Now, it works correctly.

Dim dateNow As Int = DateTime.GetMonth(DateTime.Now)
Msgbox(dateNow, "Month Now") - This works as expected.

selectedDate = DateTime.DateParse(txtDate.Text)
Dim Month As Double = DateTime.GetMonth(selectedDate)
Msgbox(Month, "Month") - Always returns 1, whatever the month.

What am I missing, please?
 

SeaBee

Member
Licensed User
selectedDate should be Long not Int:
B4X:
Dim selectedDate As Long
I have this in Sub Globals:

Private selectedDate As Long

This line:

Dim dateNow As Int = DateTime.GetMonth(DateTime.Now)

was originally a Long, but I was experimenting to see if I could break it, but it still worked.

Incidentally,

Msgbox("Text date = " & DateTime.Date(selectedDate), "")

does return the full correct date according to this line earlier:

DateTime.DateFormat = "dd/mm/yyyy"
 
Upvote 0

SeaBee

Member
Licensed User
Please use [code]code here...[/code] tags when posting code.

If it still doesn't work for you then please post the full code.
Sorry - tried <Pre>... </Pre> which I am used to elsewhere, and it didn't work.

The test Sub I wrote is this:

B4X:
Private Sub SetDate
   
    DateTime.DateFormat = "dd/mm/yyyy"
   
    Dim selectedDate As Long = DateTime.DateParse(txtDate.Text) 
'EditText view txtDate contains 27/06/2016
    Msgbox(txtDate.Text, "") 'returns 27/06/2016
    Msgbox("Selected date = " & selectedDate, "Date Long") 'returns 1453845960000
   
    Dim Month As Int = DateTime.GetMonth(selectedDate) 
    Msgbox(Month, "Month number") 'returns 1
    Msgbox("Text date = " & DateTime.Date(selectedDate), "Date from Long") 'returns 27/06/2016
   
End Sub
 
Upvote 0
Top