Android Question Date problem with 29th October

BobsYourUncle

Member
Licensed User
Longtime User
Hi,

When I add 28 or 29 days to the 1st October, I get the same result of 29th October! Works fine adding to the 1st September.

Example code below. Any ideas what's going on?

B4X:
Dim StartOfMonth As Long
Dim Date1 As Long
Dim Date2 As Long
 
DateTime.DateFormat = "dd-MM-yyyy"
 
StartOfMonth = DateTime.DateParse("01-10-2017") 
 
Date1 = StartOfMonth + (DateTime.TicksPerDay * 28)
Date2 = StartOfMonth + (DateTime.TicksPerDay * 29)
 
Log(DateTime.Date(Date1)) 'Result 29-10-2017
Log(DateTime.Date(Date2)) 'Result 29-10-2017
 

DonManfred

Expert
Licensed User
Longtime User
You should use Period and add it to the Date.

https://www.b4x.com/android/help/dateutils.html

B4X:
    Dim StartOfMonth As Long
    Dim Date1 As Long
    Dim Date2 As Long
    DateTime.DateFormat = "dd-MM-yyyy"
    StartOfMonth = DateTime.DateParse("01-10-2017")

    Dim p1 As Period
    Dim p2 As Period
    p1.Initialize
    p1.Days = 28
    p2.Initialize
    p2.Days = 29
    Date1 = DateUtils.AddPeriod(StartOfMonth,p1)
    Date2 = DateUtils.AddPeriod(StartOfMonth,p2)

    Log(DateTime.Date(Date1)) 'Result 29-10-2017
    Log(DateTime.Date(Date2)) 'Result 30-10-2017

Logger verbunden mit: 9885e6514556383552
--------- beginning of crash
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
29-10-2017
30-10-2017
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Perhaps, as per your locale settings, it is taking into account the winter hour change (-1) that will take place precisely on October 29th at 03:00:00 (at least this is in most of Europe I think)

So, 1/10/2017 (let's suppose it assumes at 00:00:00) + 28 days ---> 29/10/2017 at 00:00:00
1/10/2017 (let's suppose it assumes at 00:00:00) + 29 days ---> 30/10/2017 at 00:00:00 minus 1hour --> 29/10/2017 at 23:00:00

to me it makes some sense. ;)
 
Upvote 0

BobsYourUncle

Member
Licensed User
Longtime User
Thanks for such a quick reply Manfred. Your code works a treat!
You should use Period and add it to the Date.

https://www.b4x.com/android/help/dateutils.html

B4X:
    Dim StartOfMonth As Long
    Dim Date1 As Long
    Dim Date2 As Long
    DateTime.DateFormat = "dd-MM-yyyy"
    StartOfMonth = DateTime.DateParse("01-10-2017")

    Dim p1 As Period
    Dim p2 As Period
    p1.Initialize
    p1.Days = 28
    p2.Initialize
    p2.Days = 29
    Date1 = DateUtils.AddPeriod(StartOfMonth,p1)
    Date2 = DateUtils.AddPeriod(StartOfMonth,p2)

    Log(DateTime.Date(Date1)) 'Result 29-10-2017
    Log(DateTime.Date(Date2)) 'Result 30-10-2017
 
Upvote 0

BobsYourUncle

Member
Licensed User
Longtime User
Yes! That does make sense! Thank you. (I guess the AddPeriod function @DonManfred mentions takes this into account somehow).

Perhaps, as per your locale settings, it is taking into account the winter hour change (-1) that will take place precisely on October 29th at 03:00:00 (at least this is in most of Europe I think)

So, 1/10/2017 (let's suppose it assumes at 00:00:00) + 28 days ---> 29/10/2017 at 00:00:00
1/10/2017 (let's suppose it assumes at 00:00:00) + 29 days ---> 30/10/2017 at 00:00:00 minus 1hour --> 29/10/2017 at 23:00:00

to me it makes some sense. ;)
 
Upvote 0
Top