# Difference Between Two Dates

#### dvanwig

##### New Member
:sign0085:
Hi, Can someone help describe how I can calculate the number of days between two dates? Right now I can compare two dates but I don't know how to calculate the number of days between two dates. I'm trying to create a program that will remind me of birthdays and anniversaries 10 days before the event. Thanks for your help.:

#### specci48

##### Well-Known Member
Hello dvanwig,

the DateAdd function is a powerful way for every date calculation.

You should code something like this:
B4X:
``tenDaysBeforeBirthDayInTicks = DateAdd(bithdayInTicks, 0, 0, -10))``

specci48

#### Erel

Staff member
Another solution is to use the cTickPer... constants:
B4X:
``DaysBetween = Int((Date2-Date1)/cTicksPerDay)``

#### dvanwig

##### New Member
Thank you Specci48 and Erel for the sugestions. #### XerVision

##### Member
Dates and Days

I am attempting to use Erel example in my code. Previously , I was using DateDayofYear- which means that my program would only run on aper year basis. (Lol)

So now I have questions about using the
Int((DATE-DATE)/cTicksPerDay)

Case1- I understand if the date is the first date is x and the second is x
then it returns -1 for days in between... that is cool

Case2- for x and x+1 it returns 0 days in between ... got it.

Case 3- for x and x+2 returns 1 day in between

Now what happens if the date captured is at 11:59pm one day and 12:00am the next ... will it return 0 or -1?

I am just trying to see if there are any quirks, I do know how to parse the ticks data.

#### specci48

##### Well-Known Member
Hi XerVision,

there must be something wrong with your implementation of Erel's formula.
Can it be that you are doing a subtract of -1 somewhere else in your code?

Because in
Case1 (Date1 = Date2) Erel's function returns 0
Case2 (Date1 + 1 = Date2) Erel's function returns 1
Case3 (Date1 + 2 = Date2) Erel's function returns 2

If you try
B4X:
``````   D1 = DateParse ("08/01/2007")
D1 = D1 + 23 * cTickPerHour
D1 = D1 + 59 * cTicksPerMinute
D1 = D1 + 59 * cTicksPerSecond
D2 = D1 + cTicksPerSecond
Label1.Text= Int((D2 - D1)/cTicksPerDay)``````
Label1.Text is 0.

specci48

#### Erel

Staff member
Here is another small tip,
if you like to assign d1 the following date and time: 08/01/2007 14:30.
B4X:
``````    d1 = DateParse("08/01/2007") + (TimeParse("14:30") mod cTicksPerDay)
msgbox(date(d1),time(d1)) 'just to see the result.``````

#### XerVision

##### Member
Thanks, Guys

CALVLUE and USERSTARTDATECAL_g are in Ticks

OffSet=1
'Get the True day index regardless of time
date1= date(CALVLUE)
date2=date(USERSTARTDATECAL_g)
newdate1=DateParse(date1) + (TimeParse("00:00") mod cTicksPerDay)
newdate2=DateParse(date2) + (TimeParse("00:00") mod cTicksPerDay)
if newdate1 >= newdate2 then
TheIndex = Int((newdate1-newdate2)/cTicksPerDay)+OffSet
else
Msgbox ("Please Enter A Date On or After Your Start Date of " &Date(USERSTARTDATECAL_g), Pname\$, cMsgBoxOK)
TheIndex=-1
end if
Return TheIndex

:sign0060: It works!!!