B4J Question DateTime increment problem

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I have a date picker on a form together with a left and right arrow to reduce or increase the date picker date by 1 day.
< datePicker1 >

The issue is that the date picker will not increase past yesterdays date when using the arrows.

If you start out at today an increase the date with the right arrow - OK
Start at today and decrease the date with the left arrow - OK
Start at today, reduce the date to yesterday then try to increase the date - won't increase past yesterday.

I can use the date picker to select today again and increase the date past today - OK

The routine just sets datePicker1.ticks to the last date +/- the ticks in 1 day.
For some reason it gets stuck at yesterdays date when trying to increase.

Please see attached .zip with an example.

Any advise gratefully appreciated.
 

Attachments

  • DateCheck.zip
    2.5 KB · Views: 359

atiaust

Active Member
Licensed User
Longtime User
There is still a problem with DateTime.Date(datePicker1.DateTicks) converting and showing the wrong date.

Using Erel's suggestion below works fine and allows to step forward thru the dates.
Similar sub to go backward subtracting 1 day at a time.
B4X:
sub daysForward
daysFwd = daysFwd + 1
    Dim p As Period
    p.Days = daysFwd
    Dim tomorrow As Long = DateUtils.AddPeriod(DateTime.Now, p)
    datePicker1.DateTicks = tomorrow
End Sub

In the datePicker1_ValueChanged (Value as Long) sub
displayDate is a string

B4X:
Sub datePicker1_ValueChanged (Value As Long)
    datePicker1.DateFormat ="dd/MM/yyyy"
    displayDate = DateTime.Date(datePicker1.DateTicks)
    Log("Display date after change = "&displayDate)
End Sub

While stepping backward from today the Log shows:

Display date after change = 04/04/2017
Display date after change = 03/04/2017
Display date after change = 01/04/2017

Yet the datePicker1 shows on the screen the correct dates 04/04/2017, 03/04/2017, 02/04/2017 etc

Once datePicker1 and displayDate get out off step with each other then my database searches are out by 1 day

This is a problem and I can't see a way around it because the only way of checking the difference in the dates is visually....

Any help on this is gratefully appreciated.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,
Sorry for the delay in getting back to you.

I modified the small app that I wrote yesterday to test the date changes you suggested and it worked as it should without skipping a day.

I copied the same code into my main app that was having the problem and the issue reappeared. After many changes, tests, coffee, head scratching and curse words, I finally found what appears to be the source of the problem.

Somewhere in one of the many posts on the form, I read that you needed to set the time zone.. so I did. In the appStart.

DateTime.SetTimeZone(DateTime.TimeZoneOffset)

Once I found this and commented it out the problem disappeared. I don't know what effect this has but i seems OK without it.

Thanks for your assistance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Somewhere in one of the many posts on the form, I read that you needed to set the time zone.. so I did. In the appStart.

DateTime.SetTimeZone(DateTime.TimeZoneOffset)
This is indeed not needed and is a mistake.
You are forcing the timezone to be constant all over the year, meaning that DST changes will not be applied correctly.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…