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:
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:
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:
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?
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