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

Daestrum

Expert
Licensed User
Longtime User
Not seeing that problem here, date increments from yesterday fine.
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
Hi!
I am not so sure, i have been playing with your file and it actually allowed me to increment after i put it on yesterday.

But it was causing me an strange error, when i decrement from 04/03 instead of 04/02 it goes directly to 04/01, despite the ticks being good. (may be because today we changed for summer time).

I reproduced your error with creating a date with time 00:00:00, i actually couldnt go beyond 2 days decrement and increment, so that makes me think that may be the ticks of the datepicker is rounding.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Guys,

Thanks for the feed back.

I too am having the same problem with it skipping a day going backwards also blocking me stepping forward past yesterday.
This routine was working correctly before 5.0 last week (I think).
I have been trying to figure out what is happening since yesterday. I am in Australia and we went off Daylight saving early Sunday morning.

I can't say whether it is timezone related or V5.0. I don't have anything with 4.70 to test.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Further:

Extract of code with logs to show the actual tick values in the left arrow sub..
B4X:
   Log("dpBookings ticks before = "&dpBookings.DateTicks)
    Dim t As Long = DateTime.TicksPerDay
    Log("Ticks per day = "&DateTime.TicksPerDay)
    dpBookings.DateTicks = dpBookings.DateTicks - t
    Log("dpBookings.DateTicks - t = "&dpBookings.DateTicks)
    displayDate = Utils.TicksToMysqlDate(dpBookings.DateTicks)
    LogDebug("noteBack DisplayDate = "&displayDate&" dpBookings = "&DateTime.Date( dpBookings.DateTicks))

Going backwards the log extract shows the date skips 2 days between 2017-04-03 and 2017-04-01


* Increase date + 1 day
noteForward DisplayDate = 2017-04-04 dpBookings = 2017-04-04
Display Date = 2017-04-04 dpBookings = 2017-04-04

* Decrease -1 day
dpBookings ticks before = 1491228000000
Ticks per day = 86400000
dpBookings.DateTicks - t = 1491141600000
noteBack DisplayDate = 2017-04-03 dpBookings = 2017-04-03
Display Date = 2017-04-03 dpBookings = 2017-04-03

* Decrease -1 Day
dpBookings ticks before = 1491141600000
Ticks per day = 86400000
dpBookings.DateTicks - t = 1491051600000 <------------------ should be 1491055200000
noteBack DisplayDate = 2017-04-01 dpBookings = 2017-04-01
Display Date = 2017-04-01 dpBookings = 2017-04-01
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It may be relevant, your code has
B4X:
Dim t As Int = DateTime.TicksPerDay

Surely this should be
B4X:
Dim t As Long = DateTime.TicksPerDay

I cannot get mine to skip dates (just realised I'm running on java 9 - maybe it makes a difference)
 
Last edited:
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
I had already changed it to Long from Int but makes no difference.
DateTime.TicksPerDay is 86,400,000 milliseconds and an Int will take up to 2,147,483,647

I will try Enrique's suggestion - maybe 2 hours may work. You are correct it is 1 hour exactly.. 3,600 seconds.
There must be a rounding error or something happening.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Just managed to get a similar error - if I click the button really fast I can get a double day.
(I am calculating the difference between the current date and the new date, if it's <> ticksperday I log an error)
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
I found that by adding 't = t + DateTime.TicksPerHour * 2' to the increment sub allows going forward past yesterday

and t = t - DateTime.TicksPerHour * 2 allows going backward without skipping a day.

Seeing how I just increasing or decreasing days this will get around the problem. The must be a bug there somewhere?
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
May not be necessarily a bug, the problem is that dateTicks and Datetime assume many variables, for example: daylight, leap-year day, Time-Zone, region, etc. etc.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The routine just sets datePicker1.ticks to the last date +/- the ticks in 1 day.
The ticks per day value is only correct on days with 24 hours. It is incorrect on two important days where the DST changes.

You should use DateUtils.AddPeriod to add or substract periods from dates.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
I am not sure what is causing the problem.

I am reading a Mysql database and retrieving records that match the date selected in the datepicker by increasing or decreasing the date with the arrows.
This was working fine a couple of weeks ago when I last worked on it. Its only looking at whole dates, doesn't care about time.
I first noticed the problem over this weekend when the data retrieved from the database was 1 day less than the date displayed in the date picker.
So the date picker displays 2017-03-31 yet the data displayed is 2017-03-30. The mysql date is stored as 'Mysql Date format as a string "2017-03-30" then converted
using - Dim ticks As Long = DateTime.DateParse(mysqlStringDate).

Still working on it.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

Should that be something like datePicker1.DateTicks = datePicker1.DateTicks + DateUtils.AddPeriod(DateTime.TicksPerDay,1) ?
What is the 'period' paramater?

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Erel,

Can you please provide an example of how to subtract 1 day from today using DateUtils.AddPeriod

I am totally lost on this.

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks,

How about subtracting one day to find yesterday?

This is were the discussion started. I need to subtract one full day from today (step date backwards)
 
Upvote 0
Top