Android Tutorial [B4X] DateUtils - Simplifies Date and Time Calcuations

Erel

B4X founder
Staff member
Licensed User
Longtime User
PeriodBetween (and PeriodBetweenInDays) are usually used to calculate larger periods. So whether it truncates the milliseconds or rounds them is usually not important.

For short periods you can just subtract one value from the other. This will return the number of milliseconds. It should be simple to get the other values from that.
 

Ksmith1192

Member
Licensed User
Longtime User
Is it possible to use this library to add a certain number of days to a set date?
Say the user enters 9/23/2013, and i need to add 42 days to it, it should come out to Nov 4 2013.
 

Ksmith1192

Member
Licensed User
Longtime User
thanks erel!

I do have a problem though. I added my code like so
B4X:
    Fulldate = MonthSpn.SelectedItem & "/" & DaySpn.selectedItem & "/" & YearSpn.selectedItem
    Dim ticks As Long = DateTime.DateParse(Fulldate)
    Dim p As Period
    p.Days = 42
    Dim after42Days As Long = DateUtils.AddPeriod(p)

and i get this error message
Error description: Missing parameter.
Occurred on line: 84
Dim after42Days As Long = DateUtils.AddPeriod(p)
Word: )
 

Mahares

Expert
Licensed User
Longtime User
Isn't easier to just use this:
B4X:
Dim s As String = "9/23/2013"
Dim In42days As Long=DateTime.Add(DateTime.DateParse(s),0,0,42)
Msgbox(DateTime.Date(In42days),"") 'displays 11/04/2013 [/code/
 

Ksmith1192

Member
Licensed User
Longtime User
Well, I have an edit text box that the user can put in their exact(ish) date that they want to use. So it wouldn't be just for 9/23.
 

Mahares

Expert
Licensed User
Longtime User
Here is what you do in that case:
B4X:
Dim edt As EditText
Dim x As Int        'x can be any number of days
Dim InXdays As Long=DateTime.Add(DateTime.DateParse(edt.Text),0,0,x)
Msgbox(DateTime.Date(InXdays),"")
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
My mistake. It should be:
B4X:
Dim after42Days As Long = DateUtils.AddPeriod(ticks, p)
 

LucaMs

Expert
Licensed User
Longtime User
I state that I have not read all the posts (they are many); maybe some members have already written what I'll do.

I have small suggestions:

you could add some functions that uses the Period, or that require parameters of type Period.

Or conversions to and from Period.

I would also add ideal functions for the db, which, in my opinion, are in the format yyyy / mm / dd.

Ciao


P.S. Erel, to save you time, maybe I will write some function, I put it here, and you add them
 

LucaMs

Expert
Licensed User
Longtime User
Not sure that I understand. Which functions are you looking for?

There isn't a single format for dates. You can use DateTime.DateFormat to match your database format.


I have not used this module, at the moment, but I only noticed the Period type; it is returned from a couple of functions, but is not used as an input parameter in any function, so I just figured that it can be useful, sometimes, to have functions that require it in input. Of course, I could be wrong, maybe developing this need not happen.

For "db dates", use DateTime.DateFormat at every point of the project and in a different way is less convenient and readable than using appropriate functions.

I try to explain.

To sort by date as a criterion, I have not found a better way of use the yyyy/mm/dd format (unfortunately, losing the advantage of the db "internal" functions).

Then, I'll have to use dates in the GUI in other formats: sometimes MM / dd, other dd / MM and then convert them to "db format".

Then, to make calculations, you must use the long and then other conversions.

Maybe I'm too twisted and I have not found the perfect method.
 

LucaMs

Expert
Licensed User
Longtime User
AddPeriod expect a Period structure. Though you should first learn the module

..."Though you should first learn the module"... Surely

I had forgotten ...
maybe I was wrong something (first steps with B4A) or maybe it was a setting of version 1.8, but I had problems with DateTime.DateFormat.

I do not remember exactly; I could use it only once for the entire project or I could not set it using a variable (eg DateTime.DateFormat = MyDateFormat)
 

fotosettore

Member
Licensed User
Longtime User
hi erel
i cannot find the way to use DateUtils to check if actual time is included between a start time and end time.
example :
start time is 18:00 - end time is 23:00 - now is 19:00 : in this case variable GardenLamp must be true
if now is 16:00 variable GardenLamp must be false.
may i use dateutils in some way or do i need use something else ?
many thanks
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…