Android Question DateDialog Problem with date selection

WebQuest

Active Member
Licensed User
Hi community I am using a dated dialog to set dates in my app. at the first start I can select a date from the calendar receiving the selected date, but if I try to reopen the calendar and select a new date, a different date is returned to me than the one I have accurately selected if I select 28.12.2020 it will be returned to me 27.12. 2020, maybe something wrong with the code?

Code:
Sub BTSelectDateScadenza_Click
    
    dd.DateTicks = DateTime.Now
    Dim sf As Object = dd.ShowAsync("", "Imposta data Scadenza", "Conferma", "Annulla", "", Null, False)
    Wait For (sf) Dialog_Result(Result As Int)
    If Result = DialogResponse.POSITIVE Then
        DateTime.DateFormat = "dd/MM/yyyy"
        sDataScadenza = DateTime.Date(dd.DateTicks)
        LData_Scadenza.Text=sDataScadenza
        Log("data Scadenza: "&sDataScadenza)
        dd.DateTicks=DateTime.Now
    End If
End Sub
 

Mahares

Expert
Licensed User
Longtime User
a different date is returned to me than the one I have accurately selected
I think you are better off using the B4XDateTemplate, need the XUI Views lib checked. Here is a complete project code you can paste in a new project. It will work for you each time.
B4X:
Sub Globals
    Private Dialog As B4XDialog   'need XUI Views  checked (internal lib)
    Private DateTemplate As B4XDateTemplate
    Private XUI As XUI 
    Private btn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)  'you can use B4Xpages if you want
    Activity.LoadLayout("main")   'has a button
    Dialog.Initialize (Activity)
    Dialog.Title = "Select a date"
    DateTemplate.Initialize
    DateTemplate.MinYear = 2016
    DateTemplate.MaxYear = 2030
    DateTime.DateFormat= "dd/MM/yyyy"
End Sub

Sub btn_Click
    Wait For (Dialog.ShowTemplate(DateTemplate, "", "", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Dim MyDate As String =$"${DateTime.Date(DateTemplate.Date)}"$
        Log(MyDate)
    End If   
End Sub
 
Last edited:
Upvote 0

Albert Kallal

Active Member
Licensed User
Ah, this really fun!

Ok, keep the following rules in mind:

Datetime.now includes date AND time!!

But the default android (and most) date pickers?

The return ONLY the date – without the time portion!

And it gets better!

If you shove in “now” to a time picker? Then it works, it defaults and shows the current time. But AGAIN when you select the time the date portion is REMOVED!

So, keeping the above rules in mind?

You are shoving a date+ time into the date picker. When you “pick” you get back a date only – and thus the incoming value you passed will NOT equal the output value – even for the same day!!

As a result? Use “much” caution with “now”. If you have a database, and by accident shove in a date + time for say a simple invoice date?

Then a query that says

SELECT * from tblInvoices where invoiceDate = “2020-12-28” say for today? It will fail on you – because you started messing around with “now” as a concept and NOT as today as a date without time! And all your date values in that database may now well have a time part!!! - all you code will seem to not work for a simple date query.

Turns out that both the built in datetime and the dateutils library (which I recommend) are missing this all important feature/thing/property. (they are missing “today” as date only value!

So, in place of “now”. you want to use this:

B4X:
DateUtils.SetDate(DateTime.GetYear(DateTime.Now),DateTime.GetMonth(DateTime.now),DateTime.GetDayOfMonth(DateTime.Now))

My sincere apologies for posting that “thing” above. I mean, of course I don't suggest that all over the place any old time you need "today" as a date, one would type in that above work of art.

So, I place in my global grab bag of handy routines this:

B4X:
Public Sub GetTodayT As Long
   
    ' returns today as date without time part.
   
    Dim tDay As Long = DateTime.Now
   
    Return DateUtils.SetDate(DateTime.GetYear(tDay),DateTime.GetMonth(tDay),DateTime.GetDayOfMonth(tDay))
End Sub

So, for your code to now spit out the SAME date as incoming to the date picker, and outgoing?

Then change:
B4X:
dd.DateTicks = DateTime.Now
to
dd.DateTicks = MyCode.GetTodayT

So just be aware that now includes the current time – and often you don’t want it to be involved with dates.

But then again, often you do!

So, if I use a time picker?
Again, you can free shove in "now".

However, on the way back out? Your date part will be gone!

So, for a time picker to get say today WITH the date?

B4X:
    Dim tToday as long = MyCode.GetTodayT
    Dim p As Period
    p.Initialize
    Dim PopTime As TimeDialog
    PopTime.TimeTicks = datetime.now

    If PopTime.Show("","Start Time","Ok","Cancel","",Null) = DialogResponse.POSITIVE Then
        p.Hours = PopTime.Hour
        p.Minutes = PopTime.Minute

        Dim DateAndTime As Long = DateUtils.AddPeriod(tToday,p)

And for the most part? Quite sure if you use other suggested widgets, time pickers or what not? You still deal with above.

I suppose one way to really get around these issues is to use some widget that ALWAYS gives the user a date and a time to select in one shot - and thus no confusing will exist.

But, if you need "today" then I in most cases WILL suggest that you get today as ticks, but without the time part - and datetime.now gives you date + time.


Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0

WebQuest

Active Member
Licensed User
Hi it seems strange to me that an internal library such as dateDialog returns a different tick from the selection. I repeat if I click 31/12/2020 it always returns me one day before 30/12/2020 or 20/12/2020 and returns 19/12/2020 the same thing happens with the selection of the time if I select 10:30 it returns me 09:30. I believe that Datetime.now does not matter, I believe it is a layout problem not reinitialized. If anyone has had the same problem and solved it please help me.
 
Upvote 0

WebQuest

Active Member
Licensed User
I set up a log to receive the date and the time is if I select 31/12/2020 it returns me 30/12/2020 23:00:00. That is the question! By clicking on the date selector you scale by one hour entering the day before. Anyone know how to initialize time if not selected on 00:00:00?
 
Upvote 0

Albert Kallal

Active Member
Licensed User
Hum, you sure that your time zone setting not been messed with?

I looks that you perhaps seeing a offset here?
What does this give you?

B4X:
Log(DateTime.GetTimeZoneOffsetAt(DateTime.Now))


Not 100% clear what dd is of what type and what lib you are using?

I grabbed the sample link from here: - you using this one?
https://www.b4x.com/android/forum/threads/27548

When I run above - pick a date - I don't see nor get that off set.
(mind you - that is rather a old one).

You might want to post a sample app (file->export as zip) that shows this.

But I not seeing the offset you speak of - but then again, I might have pulled the wrong library, or one that is different then what you are using?

My spider sense suggest that some time zone issue is at play here.

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…