Android Question Noob date question

Douglas Farias

Expert
Licensed User
Longtime User
Hi all
sory for this noob question xD
but how ti calculate a date - date?

i need to calculate my dog years old

how i can calculate datetime.now - dog birth?

My dog is .... years old
Years:
Mounths:
weeks:
Days:

i m already take a birth with

B4X:
    Private pa As Phone
    data.Year = DateTime.GetYear(DateTime.Now)
    data.Month = DateTime.GetMonth(DateTime.Now) 
    data.DayOfMonth = DateTime.GetDayOfMonth(DateTime.Now)
    If pa.SdkVersion >= 11 Then data.ShowCalendar = False
    data.Show("", "", "OK", "Cancelar", "", Null)
    Log(data.DayOfMonth)
    Log(data.Month)
    Log(data.Year)


but how to calculate and with time.now and show

My dog is .... years old
Years:
Mounths:
weeks:
Days:

??? thx to all
 

Reviewnow

Active Member
Licensed User
Longtime User
Should work
B4X:
Sub CalculateDogYears(dob As String) As String
Dim p As Period = DateUtils.PeriodBetween(DateTime.DateParse(dob),DateTime.Now)
'Converted From Source http://www.b4x.com/android/forum/threads/calculating-age.44280/
'Thanks to DonManfred
    Log((p.years * 7) &" Years, "&p.Months&" months, "&p.Days&" days")
  ' Add 7 years for each year a dog is alive..
if p.years  = 0 then
  return (.58333 * p.Months) &" Years, "&p.Months&" months, "&p.Days&" days"
else
    Return (p.years * 7) &" Years, "&p.Months&" months, "&p.Days&" days"
end if

End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@Reviewnow how to return the weeks?

Calculate the weeks from month and days... It´s simple mathematic

You can even start from one day and in a while-loop you increment one day. Everytime you pass the starting-weekday you increment weeks by one
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
@DonManfred
thx man it works

the last question, how can i calculate date + days?
have have a date for example 09/27/2014 i want add + 65 days

how can i make 09/27/2014 + 65 ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    DateTime.DateFormat = "mm/dd/yyyy" '09/27/2014
    Dim dt As Long = DateTime.DateParse("09/27/2014")
    Log("start="&DateTime.Date(dt))
    Dim p As Period
    p.Initialize
    p.Days = 65
 
    Dim destdt As Long = Dateutils.AddPeriod(dt,p)
    Log("end="&DateTime.Date(destdt))
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
i have try your code but this show me wrong result
start=09/27/2014
end=09/02/2014
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
fixed
B4X:
Dim la As String
Dim p As Period
p.Days = 65
Dim SomeDate As Long = DateTime.DateParse("01/01/2010")
Dim NewDate As Long = DateUtils.AddPeriod(SomeDate, p)
la = DateUtils.TicksToString(NewDate)
la = la.SubString2(0,10)
Log(la)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    DateTime.DateFormat = "dd.MM.yyyy" '27.09.2014
    Dim dt As Long = DateTime.DateParse("27.09.2014")
    Dim destdt As Long = dt + (65 * DateTime.TicksPerDay)
    Log("start="&DateTime.Date(dt))
    Log("end="&DateTime.Date(destdt))

The error was a lowercase mm (minutes) instead of MM (Month).
So the above code from me should work...
 
Upvote 0
Top