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

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

  1. sdujolo

    sdujolo Member Licensed 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: Sep 17, 2013
  2. Erel

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

    Ksmith1192 Member Licensed 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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Sure. Something like:
    Code:
    Dim ticks As Long = DateTime.DateParse(...)
    Dim p As Period
    p.Days = 
    42
    Dim after42Days As Long = DateUtils.AddPeriod(p)
     
  5. Ksmith1192

    Ksmith1192 Member Licensed User

    thanks erel!

    I do have a problem though. I added my code like so
    Code:
    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: )
     
  6. Mahares

    Mahares Well Known Member Licensed User

    Isn't easier to just use this:
    Code:
    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/
     
    johnB and tdocs2 like this.
  7. Ksmith1192

    Ksmith1192 Member Licensed 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.
     
  8. Mahares

    Mahares Well Known Member Licensed User

    Here is what you do in that case:
    Code:
    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),"")
     
    tdocs2 likes this.
  9. Erel

    Erel Administrator Staff Member Licensed User

    My mistake. It should be:
    Code:
    Dim after42Days As Long = DateUtils.AddPeriod(ticks, p)
     
  10. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Is there any way that one could retrieve the current date and time settings of a Tablet?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    What do you mean with date and time settings?

    Do you want to get the current date and the time zone?
     
  12. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Want to get/read the date and time that my tablet is set to.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Log(DateUtils.TicksToString(DateTime.Now))
     
  14. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Perfect! Thanks Erel (baie dankie Erel)!
     
  15. LucaMs

    LucaMs Expert Licensed 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 :)
     
  16. Erel

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

    LucaMs Expert Licensed User


    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.
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    AddPeriod expect a Period structure. Though you should first learn the module ;)
     
  19. LucaMs

    LucaMs Expert Licensed User

    ..."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)
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    There are no problems with DateTime.DateFormat. You can set it with any valid string you like (it doesn't matter whether it is a variable).
     
Loading...
  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