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

Discussion in 'Tutorials & Examples' started by Erel, Feb 12, 2013.

  1. kostas3001

    kostas3001 Member Licensed User

    The problem is on: DateTime.DateTimeParse(d, t)


    Dim ticks As Long = DateTime.DateTimeParse(d, t)
    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
  2. kostas3001

    kostas3001 Member Licensed User

    Hi Erel

    Your example, DateTimeExample, has the same problem.
    django likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    V1.02 uploaded to first post. Fixes the above issue.
  4. kostas3001

    kostas3001 Member Licensed User

    Hi Erel

    Now all is OK.

    Thank you.
  5. RiverRaid

    RiverRaid Active Member Licensed User


    Hi Erel!

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

    Thank you :)
  6. Erel

    Erel Administrator Staff Member Licensed User

    It should be simple to extract weeks from the PeriodBetween method. You just need to divide the days by 7.
  7. mjtaryan

    mjtaryan Active Member Licensed User


    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.

  8. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code in your module.
    DateUtils is now a library that is included in Basic4android.

    Instead of copying the code you can reference this library and extend it.
  9. Creideiki

    Creideiki Member Licensed User

    Activity Context


    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?
  10. Erel

    Erel Administrator Staff Member Licensed User

    This is a bug related to code modules compiled as libraries. It is fixed for the next version. Instead of using the library, you should add the code module to your project (make sure to remove the library reference).
  11. Creideiki

    Creideiki Member Licensed 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?
  12. Erel

    Erel Administrator Staff Member Licensed 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.
  13. Erel

    Erel Administrator Staff Member Licensed User

    V1.05 released. New SetDateAndTime2 method. This method allows you to explicitly set the time zone of the set time instance.
  14. JOTHA

    JOTHA Well-Known Member Licensed User

    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 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:
    Dim Datum_HEUTE_in_Ticks, Datum_TERMIN_in_Ticks, ERGEBNIS_TERMIN_WOCHENTAG As Long
    Datum_HEUTE_in_Ticks = 
    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) ...
    ... 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?
  15. Erel

    Erel Administrator Staff Member Licensed 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.
  16. JOTHA

    JOTHA Well-Known Member Licensed User

    Thank you Erel,

    I found a solution for my problem:
    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 = 
    Datum_TERMIN_in_Ticks = 
  17. JOTHA

    JOTHA Well-Known Member Licensed User

    ... yes, I found that way ... but not how to edit the signature ... ?
  18. Erel

    Erel Administrator Staff Member Licensed User

    Your code is not correct. I'm not sure what you are trying to do. You should either use DateTime.ParseDateAndTime or DateUtils.
  19. Rick Harris

    Rick Harris Well-Known Member Licensed User

    The comment made above that "Starting from B4A v2.70, DateUtils is included as a library in the IDE." does not seem to be correct. The libary only works if the "DateUtils.bas" module is deliberately added to the project (I am using B4A version 2.71).
  20. Erel

    Erel Administrator Staff Member Licensed User

    Have you checked DateUtils library in the libraries list?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice