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

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

  1. Croïd

    Croïd Active Member Licensed User

    I save the time spent and add the new time fort accumulate
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Do you want to add two Period objects?

    You can add them to the current time with DateUtils.AddPeriod (once for each period). Then use DateUtils.PeriodBetween to find the period between the calculated date and now.
     
  3. Croïd

    Croïd Active Member Licensed User

    sorry, but I'm stuck at this level!

    addtime = DateUtils.AddPeriod(now, diff)
     

    Attached Files:

    • add.zip
      File size:
      7.3 KB
      Views:
      120
  4. Erel

    Erel Administrator Staff Member Licensed User

    What are you trying to calculate?
     
  5. Croïd

    Croïd Active Member Licensed User

    Last edited: Nov 10, 2014
  6. Erel

    Erel Administrator Staff Member Licensed User

    Don't confuse a period (diff) with a time value. They are not the same thing.

    You cannot add two time values. The result will be meaningless.

    You can add two periods to a time value.
     
  7. Croïd

    Croïd Active Member Licensed User

    Ah ! Ok thanks
     
  8. chrjak

    chrjak Active Member Licensed User

    Ho guys!

    Is it possible to count down with some days left out? for example friday is left out. Then the time should be the same over the whole day.

    E.g. Thursday 00:01 am : 2 Days and 23 hours ...
    Friday 12:00 am : 2 Days and 0 minutes
    Friday 04:00 pm: 2 Days and 0 minutes
    Saturday 00:01 am: 1 Day and 23 hours...
    And so on...


    Is this possible with 2 days, too? e.g. Saturday and Sunday


    Best regards!
    Chris
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure that I understand. What is the current date and time and target date and time in your example?
     
  10. chrjak

    chrjak Active Member Licensed User

    In my example the current date is the day in front: (Friday 12:00 am : 2 Days and 0 minutes)
    the target date is monday. And then it should calculate the days by leaving some days out (for example friday) then the calculated time should be like 2 days and 0 minutes when the day is a friday and the time is 12am

    Best Regards
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Not automatically. You will need to do some calculations for this.

    The "brute force" solution is to calculate the period until Friday and then calculate the period between Saturday and the target date. You should do it in a loop which adds the periods until the period between Saturday and the target date is shorter than 5 days.
     
  12. Mashiane

    Mashiane Expert Licensed User

    Q. How do I get the week number of the year? e.g. week 131 of n?
    Q. How do I get the day number of the year? e.g. day 235 out of 365 days
     
  13. Erel

    Erel Administrator Staff Member Licensed User

  14. tdocs2

    tdocs2 Well-Known Member Licensed User

    Greetings, all.

    I do not seem to find an answer for this and it is a little baffling. I understand that DateUtils is part of the IDE (core?) since 2.7.

    I am using B4A Help 2.40 by A Graham to view the DateUtils.xml. The xml that I have for DateUtils is v 1.05

    My B4A version is 4.30.

    Code:
    DateUtils.xml - version 1.05
    Nevertheless, only Period Methods and Fields are shown in B4A Help. No DateUtils methods show in the B4A Help, like DateUtils.GetMonthsNames.

    Is it an XML issue or a B4A Help issue, or am I missing something?

    Thank you for any help.

    Sandy

    PS: I opened the DateUtils.xml in Word and the methods are there.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Probably a problem in the Help app.
     
  16. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Erel.

    Maybe Andy Graham would update (WISH). I find the B4A help a valuable tool.

    Best regards.

    Sandy
     
  17. LucaMs

    LucaMs Expert Licensed User


    Have you tried B4X object browser developed by Vader?
     
    tdocs2 and DonManfred like this.
  18. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Luca.

    I have. It is extremely complete, and it works for DateUtils. I still gravitate to Andy's B4A Help better - maybe because I am used to it.

    Ciao.

    Sandy
     
  19. Anser

    Anser Well-Known Member Licensed User

    Friends,

    Is there a Function that returns the BoM() and EoM() ie Beginning of Month and End of Month() of a given date

    To be more precise, if I provide a date as a parameter to this function it would return the Beginning date or End date of the month
    For eg. BoM('20-Feb-2015') --> 01-Feb-2015
    EoM('20-Feb-2015') --> 28-Feb-2015


    Regards
    Anser
     
  20. LucaMs

    LucaMs Expert Licensed User


    Code:
    DateTime.DateFormat = "dd-MMM-y"

    Dim MyDate As String = "20-Feb-2015"
    Dim lngMyDate As Long = DateTime.DateParse(MyDate)

    Dim Month As Int = DateTime.GetMonth(lngMyDate)
    Dim Year As Int  = DateTime.GetYear(lngMyDate)
    Dim DaysInMonth As Int = DateUtils.NumberOfDaysInMonth(Month, Year)
    Dim BoM As String = DateTime.Date(DateUtils.SetDate(Year, Month, 1))
    Dim EoM As String = DateTime.Date(DateUtils.SetDate(Year, Month, DaysInMonth))

    Log(MyDate)
    Log(BoM)
    Log(EoM)
     
    Last edited: Apr 13, 2015
    Anser, DonManfred and Erel like this.
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