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

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

  1. fotosettore

    fotosettore Member Licensed User

    hi erel
    i cannot find the way to use DateUtils to check if actual time is included between a start time and end time.
    example :
    start time is 18:00 - end time is 23:00 - now is 19:00 : in this case variable GardenLamp must be true
    if now is 16:00 variable GardenLamp must be false.
    may i use dateutils in some way or do i need use something else ?
    many thanks
     
    Last edited: Jan 12, 2014
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code:
    Code:
    Sub IsTimeBetween(StartHour As Int, StartMinute As Int, EndHour As Int, EndMinute As Int) As Boolean
       
    Dim now As Long = DateTime.now
       
    Dim st As Long = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), _
         
    DateTime.GetDayOfMonth(now), StartHour, StartMinute, 0)
       
    Dim et As Long = DateUtils.SetDateAndTime(DateTime.GetYear(now), DateTime.GetMonth(now), _
         
    DateTime.GetDayOfMonth(now), EndHour, EndMinute, 0)
       
    Return now >=st AND now <= et
    End Sub
     
  3. fotosettore

    fotosettore Member Licensed User

    AD MAIORA SEMPER, dear Erel ...!
    it works fine ;)
     
  4. Itila Tumer

    Itila Tumer Active Member Licensed User

    I have a question , How to change the days /months names for my language ??
    also changing DD/MM/YY
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. mat2175

    mat2175 Member Licensed User

    Hi Erel, I need to work out how many weekends there are between 2 dates and i'm not sure how to go about this.
    Any help would be appreciated thanks.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Log(HowManySaturdays(DateTime.Now, DateTime.DateParse("06/08/2014")))
    End Sub

    Sub HowManySaturdays(start As Long, endTime As Long) As Int
       
    Dim p As Period = DateUtils.PeriodBetweenInDays(start, endTime)
       
    Dim res As Int = p.Days / 7
       
    Dim today As Int = DateTime.GetDayOfWeek(DateTime.Now)
       
    Dim Saturday As Int = 7
       
    If today + (p.Days Mod 7) >= Saturday Then res = res + 1
       
    Return res
    End Sub
     
  8. mat2175

    mat2175 Member Licensed User

    It's just what I needed thanks Erel
     
  9. westingenieria

    westingenieria Active Member Licensed User

    hi, how to add two periods? thanks
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    What is the purpose of adding the two periods? You can call AddPeriod twice to add the two periods to a specific date.
     
  11. westingenieria

    westingenieria Active Member Licensed User

    I have two periods, period A : A.minutes = 51 A.seconds= 34. Period B: B.minutes= 23 B.seconds = 56, how add this periods?

    pd: thanks for reply
     
  12. Mahares

    Mahares Well Known Member Licensed User

    I think you are looking for something like this. Let us hope so:
    Code:
    Dim Diff, A, B As Period
        A.minutes = 
    51 :A.Seconds=34
        B.Minutes=
    23 :B.Seconds=56
        
    Dim t1 As Long = DateTime.Now
        
    Dim t2 As Long = DateUtils.AddPeriod(t1,A)
        t2=DateUtils.AddPeriod(t2,B)
        Diff=DateUtils.PeriodBetween(t1,t2)
        
    Msgbox(Diff.Hours & " hrs " & Diff.Minutes & " mn " & Diff.Seconds & " sec",""'displays: 1 hr 15 mn 30 sec
     
  13. westingenieria

    westingenieria Active Member Licensed User

    Code:
    Dim t1 As Long = DateTime.Now
    what it would be without DateTime.Now, passing the period A to ticks (long)?
     
  14. Mahares

    Mahares Well Known Member Licensed User

    You can do it like this also without the DateTime.Now. It yields the same result:
    Code:
    Dim Diff, A, B As Period
        A.minutes = 
    51 :A.Seconds=34
        B.Minutes=
    23 :B.Seconds=56
        
    Dim t2 As Long = DateUtils.AddPeriod(0,A)  'requires 2 parameters
        t2=DateUtils.AddPeriod(t2,B)
        Diff=DateUtils.PeriodBetween(
    0,t2)   'requires 2 parameters
        Msgbox(Diff.Hours & " hrs " & Diff.Minutes & " mn " & Diff.Seconds & " sec",""'displays: 1 hr 15 mn 30 sec
     
    westingenieria likes this.
  15. westingenieria

    westingenieria Active Member Licensed User

    I think it will work. Thanks Mahares ;)
     
  16. LucaMs

    LucaMs Expert Licensed User

    I confess: I have not read everything!

    DateTime.DateParse only accepts date? And time?

    It would be helpful a function like:
    DateTime.DateTimeParse("13/11/2000 12:25:05")
     
  17. LucaMs

    LucaMs Expert Licensed User

    I do not know if this is correct or it should take into account the different formats!
    Code:
    Private Sub DateTimeParse(DateTimeString As StringAs Long
        
    Private strDate As String = Regex.Split(" ", DateTimeString)(0)
        
    Private strTime As String = Regex.Split(" ", DateTimeString)(1)
        
    Private lngDate As Long = DateTime.DateParse(strDate)
        
    Private lngTime As Long = DateTime.TimeParse(strTime)
        
    Return lngDate + lngTime
    End Sub

    [P.S. I would say no. It returns wrong values]
     
    Last edited: Jul 6, 2014
  18. LucaMs

    LucaMs Expert Licensed User

    ops, solved:
    Code:
    Public Sub DateTimeParse(DateTimeString As StringAs Long
        
    Private strDate As String = Regex.Split(" ", DateTimeString)(0)
        
    Private strTime As String = Regex.Split(" ", DateTimeString)(1)
        
    Return DateTime.DateTimeParse(strDate, strTime)
    End Sub
     
  19. Croïd

    Croïd Active Member Licensed User

    how I can add ? I need to convert all in seconds ?

    Labelclocktoday.text = Diff.Hours & " hrs " & Diff.Minutes & " mn " & Diff.Seconds & " sec"

    Labelclockyesterday.text = Diff.Hours & " hrs " & Diff.Minutes & " mn " & Diff.Seconds & " sec"

    activity.title = Labelclockyesterday.text + Labelclocktoday.text !!!!!!!
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    What exactly are you trying to calculate?
     
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