Date calcuation - from here to there

hackhack

Active Member
Licensed User
Longtime User
Ok,

Task: Calculate number of days from 'today' to 6/6/2012

What I do is roughly:

(DateTime.DateParse("06/06/2012")-datetime.now)/datetime.ticksperday

That is 336

However in the calculation in my spreadsheet its 337

And this site also says 337 (without including the last day)

If I take the calculation up there, and remove the datetime.now and instead use a dateparse to parse the date from today i get 337 as well.

So is it because there is hour/minute information in datetime.now and should be removed somehow?
 
Last edited:

kanaida

Active Member
Licensed User
Longtime User
Could be some sort of rounding problem.

What data types are all of the variables uses?
using the wrong type can also cut off things like say decimal places.

If I remember right they should all be "Long" in order to hold ticks. I had a similar strange division problem when calculating an exchange rate when dividing.

Just out of curiosity, try to use "Today" instead of "Now", if it exists in B4A. I know it's in vb, but I don't remember seeing it here.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Hm, I don't understand what ceil does here, i went for this instead

B4X:
DateTime.DateParse(DateTime.Date(DateTime.Now))
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
BTW, with the new search engine it is very easy to find the meaning of a keyword.
Basic4android Search: Ceil

The description appears before the results.

You keep assuming I haven't done that. I read it. Doesn't make me understand why it works in the example above.

But I'm thinking my "solution" also works. I don't know which method would be the fastest but its not time sensitive matter in this case.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
To explain the line
B4X:
Ceil((DateTime.DateParse("06/06/2012") - datetime.now) / datetime.ticksperday)
This bit
B4X:
(DateTime.DateParse("06/06/2012") - datetime.now)
gets the difference in ticks between now and 06/06/12

This value is then divided by datetime.ticksperday to give the number of days.

Ceil then rounds the number of days UP to the next whole day.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Ceil then rounds the number of days UP to the next whole day.

Yeah, but how can we be sure that's the right value? I never got math - you'd think that would be a problem when trying to make programs.... it probably is, but some of them still work fine :D
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Lets say that today is 7/7/2011.
FutureDate = DateParse("06/06/2012") returns the ticks value of 6/6/2012 at midnight (in your time zone, however time zones are not relevant in this case).
DateTime.Now will return the ticks value of 7/7/2011 midnight + x.
x is the number of ticks since midnight today.
We know that 0 <= x < #ticks per day.

When you do (FutureDate - Now) you will get the correct value of ticks minus x.
Lets say that the number of days between the dates is 50.
After dividing the result by TicksPerDay the result will be: 50 >= result > 49.
By calling Ceil(result) the result is rounded to 50.

As I wrote above your solution is good as you are getting rid of the x value in a simpler way.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
I have another date related question though.

I'm seeing one hour difference when running the app on my phone in the emulator. In the emulator it is correct, on my phone it has one hour too many.

I'm assuming this is somehow related to daylight saving?

But when my phone is showing the clock correctly, wouldn't the app get the right time as well?
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
What code are you using to show the time?

Just a label.

I have done some testing and tried to reduce this to the bare bones.

So I made an app, the FULL source of which follows here:

Dim t2 As Long,s As String
t2=487440000

s=DateTime.GetDayOfMonth(t2) & ":" _
& DateTime.GetHour(t2) & ":" _
& DateTime.GetMinute(t2) & ":" _
& DateTime.GetSecond(t2)
Log(s)
ExitApplication

On the emulator the output is: 6:15:24:0
On the Android device the output is: 6:16:24:0


Turns out its the time zone setting of the phone which affects this.

I would say that at worst this is a bug, at best its bad documentation.
The docs for datetime says about GetDayOfMonth/GetHour/GetMinute/GetSecond that the value is "from the ticks value" - and says nothing about timezone setting interfering.
 
Last edited:
Upvote 0
Top