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

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
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
 
Reactions: omo

fotosettore

Member
Licensed User
Longtime User

AD MAIORA SEMPER, dear Erel ...!
it works fine
 

Itila Tumer

Active Member
Licensed User
Longtime User
I have a question , How to change the days /months names for my language ??
also changing DD/MM/YY
 

mat2175

Member
Licensed User
Longtime 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.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
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
 

westingenieria

Active Member
Licensed User
Longtime 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
 

Mahares

Expert
Licensed User
Longtime User
I think you are looking for something like this. Let us hope so:
B4X:
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
 

westingenieria

Active Member
Licensed User
Longtime User
B4X:
Dim t1 As Long = DateTime.Now

what it would be without DateTime.Now, passing the period A to ticks (long)?
 

Mahares

Expert
Licensed User
Longtime User
You can do it like this also without the DateTime.Now. It yields the same result:
B4X:
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
 

LucaMs

Expert
Licensed User
Longtime User
I do not know if this is correct or it should take into account the different formats!
B4X:
Private Sub DateTimeParse(DateTimeString As String) As 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:

LucaMs

Expert
Licensed User
Longtime User
ops, solved:
B4X:
Public Sub DateTimeParse(DateTimeString As String) As Long
    Private strDate As String = Regex.Split(" ", DateTimeString)(0)
    Private strTime As String = Regex.Split(" ", DateTimeString)(1)
    Return DateTime.DateTimeParse(strDate, strTime)
End Sub
 

Croïd

Active Member
Licensed User
Longtime 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 !!!!!!!
 

Croïd

Active Member
Licensed User
Longtime User
I save the time spent and add the new time fort accumulate
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…