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

DateUtils is a code module with a set of useful date and time related methods.
It complements DateTime api, it doesn't replace it.

The methods included in DateUtils:

B4X:
'Calculates the period between two date instances.
'This method returns a Period type with years, months, days, hours, minutes, seconds.
Sub PeriodBetween(Start As Long, EndTime As Long) As Period

'Calculates the period between two date instances.
'This method returns a Period type with days, hours, minutes, seconds
Sub PeriodBetweenInDays (Start As Long, EndTime As Long) As Period

'Returns the month name of the given date.
'Similar To DateTime.GetMonth which returns the month as an integer.
Sub GetMonthName(Ticks As Long) As String

'Returns the day of week name.
'Similar to DateTime.GetDayOfWeek which returns the day of week as an integer.
Sub GetDayOfWeekName(Ticks As Long) As String

'Returns a list with the week days names, using the device set locale.
Sub GetDaysNames As List

'Returns a list with the months names, using the device set locale.
Sub GetMonthsNames As List

'Returns the ticks value of the given date (the time will be 00:00:00).
Sub SetDate(Years As Int, Months As Int, Days As Int) As Long

'Returns the ticks value of the given date and time
Sub SetDateAndTime(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int) As Long

'Returns the ticks value of the given date and time with the specified time zone offset.
'The last parameter is the time zone offset measured in hours.
Public Sub SetDateAndTime2(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int, _
      TimeZone As Double) As Long

'Returns the number of days in the given month
Sub NumberOfDaysInMonth(Month As Int, Year As Int) As Int

'Adds a Period to the given date instance. Do not forget to assign the result.
Sub AddPeriod(Ticks As Long, Per As Period) As Long

'Tests whether the two ticks values represent the same day.
Sub IsSameDay(Ticks1 As Long, Ticks2 As Long) As Boolean

'Converts ticks value to unix time.
Sub TicksToUnixTime(Ticks As Long) As Long

'Converts unix time to ticks value.
Sub UnixTimeToTicks(UnixTime As Long) As Long

DateUtils includes a type named Period:
B4X:
Type Period (Years As Int, Months As Int, _
      Days As Int, Hours As Int, Minutes As Int, Seconds As Int)

You can use PeriodBetween method to get the Period between two dates.
AddPeriod method adds a Period to a given date instance.

The DateUtils module is included in the attached example.

v1.05: Adds SetDateAndTime2. Allows you to explicitly set the time zone.

v1.03: Fixes a bug in NumberOfDaysInMonth method.

v1.02: Fixes a locale related bug (SetDate and SetDateAndTime).

v1.01: Adds PeriodBetweenInDays - Similar to PeriodBetween however the years and months will be zero (days field will be larger as needed).
This version also fixes a bug in SetDateAndTime related to DST

Starting from B4A v2.70, DateUtils is included as a library in the IDE.
 
Last edited:

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
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: )
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:
Top