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:

kostas3001

Member
Licensed User
Longtime User
The problem is on: DateTime.DateTimeParse(d, t)

in

Try
Dim ticks As Long = DateTime.DateTimeParse(d, t)
Catch
DateTime.DateFormat = df
DateTime.TimeFormat = tf
Log("Error: Invalid value: " & d & " " & t)
Return "invalid date" + 1 'hack to throw an error
End Try

With Spanish languaje dasn't works
 

RiverRaid

Active Member
Licensed User
Longtime User
Weeks

Hi Erel!

Do you see a chance to integrate Weeks as well into the PeriodBetween - Function in the library?

Thank you :)
 

mjtaryan

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

Erel,

I've been working on a sismilar module eventually intended to be a library that includes other functions I believe would be useful as well. May I have permission to incorporate this module into mine giving you credit for what I use? Thanks.

Mike
 

Creideiki

Active Member
Licensed User
Longtime User
Activity Context

Hi,

why does DateUtil require an activity context? I wanted to use it in a class which is called from a service... how can I fix that?
 

Creideiki

Active Member
Licensed User
Longtime User
Many thanks for this really quick answer (it's sunday morning!). :icon_clap:
I used only two subs (SetDate and AddPeriod) which I implemented myself now.

BTW: Is there really no possibility to create the ticks from integers other than creating a string and parsing it with DateTime.ParseDate and the like? That's not very efficient, is it?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you asking about SetDate?

The first implementation was indeed different. However there are some corner cases which are (almost) impossible to solve in any other way. Note that date and time calculations are much more complicated than it may first seem. It is quite easy to build a solution that is correct %99. However the last percent (related to DST periods) is extremely difficult to get correct.

Assuming that you are not calling SetDate hundreds (or thousands) of times in a row then I don't think you will be able to measure any extra overhead.
 

JOTHA

Well-Known Member
Licensed User
Longtime User
DateUtils is a code module with a set of useful date and time related methods.
It complements DateTime api, it doesn't replace it.

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

Hi Erel,

is it possible to get DateUtils as library?
My version of B4A is 2.22 and in this version it is not included.
In the DateUtils.zip are no extra library-files included.

I need to get the "DayOfWeek" for example on 20.08.2013 (should be "3" = Thuesday).

This code is not really working:
B4X:
Dim Datum_HEUTE_in_Ticks, Datum_TERMIN_in_Ticks, ERGEBNIS_TERMIN_WOCHENTAG As Long
Datum_HEUTE_in_Ticks = DateTime.Now
Datum_TERMIN_in_Ticks = DateTime.DateParse(""&TerminDatum.Text&"")   'f.e. "20.08.2013"
ERGEBNIS_TERMIN_WOCHENTAG = Datum_TERMIN_in_Ticks - Datum_HEUTE_in_Ticks

I don't have the chance to calculate like this (in DateUtils) ...
'Returns the ticks value of the given date and time
'SetDateAndTime(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int) As Long
... because I have no "Hours", "Minutes", "Seconds" the result is sometimes right and sometimes wrong.

Thank you for your support.

P.S.: How can I update my own Profile with new info and photo?
 

JOTHA

Well-Known Member
Licensed User
Longtime User
The problem is that libraries based on B4A code are only supported in version 2.5+. You should be able to use DateUtils as a code module. Which error do you get?

Click on your user name to edit your profile.

Thank you Erel,

I found a solution for my problem:
B4X:
Dim DIFFERENZ_Uhrzeit_HEUTE_in_Ticks, Datum_HEUTE_in_Ticks, Datum_TERMIN_in_Ticks, ERGEBNIS_TERMIN_WOCHENTAG As Long
DIFFERENZ_Uhrzeit_HEUTE_in_Ticks = (DateTime.TimeParse(""&Panel_008_EditText_030_TERMIN_ZUR_OBJEKTBESICHTIGUNG_UHRZEIT_ERFASSUNG.Text&"") - DateTime.TimeParse("00.00"))
Datum_HEUTE_in_Ticks = DateTime.Now
Datum_TERMIN_in_Ticks = DateTime.DateParse(""&Panel_008_EditText_030_TERMIN_ZUR_OBJEKTBESICHTIGUNG_ERFASSUNG.Text&"")
ERGEBNIS_TERMIN_WOCHENTAG = DIFFERENZ_Uhrzeit_HEUTE_in_Ticks + Datum_TERMIN_in_Ticks
 

sdujolo

Member
Licensed User
Longtime User
Is it possible to get PeriodBetweenInDays with ms also?

I thinks it converts the secounds wrong if
1378998109578-1378998185548 = 75970 = 1minute 15 seconds and 970ms
DateUtils.PeriodBetween(1378998109578,1378998185548 )
[Days=0, Hours=0, IsInitialized=true, Minutes=1, Months=0, Seconds=15, Years=0]

Should it not return 16sec?

Can it be done like this?
Sub PeriodBetween(Start AsLong, EndTime AsLong Use_ms as Boolean) As Period
 
Last edited:
Top