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

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:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…