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

DateUtils is a code module with a set of useful date and time related methods.
It complements DateTime api, it doesn't replace it.

The methods included in DateUtils:

B4X:
'Calculates the period between two date instances.
'This method returns a Period type with years, months, days, hours, minutes, seconds.
Sub PeriodBetween(Start As Long, EndTime As Long) As Period

'Calculates the period between two date instances.
'This method returns a Period type with days, hours, minutes, seconds
Sub PeriodBetweenInDays (Start As Long, EndTime As Long) As Period

'Returns the month name of the given date.
'Similar To DateTime.GetMonth which returns the month as an integer.
Sub GetMonthName(Ticks As Long) As String

'Returns the day of week name.
'Similar to DateTime.GetDayOfWeek which returns the day of week as an integer.
Sub GetDayOfWeekName(Ticks As Long) As String

'Returns a list with the week days names, using the device set locale.
Sub GetDaysNames As List

'Returns a list with the months names, using the device set locale.
Sub GetMonthsNames As List

'Returns the ticks value of the given date (the time will be 00:00:00).
Sub SetDate(Years As Int, Months As Int, Days As Int) As Long

'Returns the ticks value of the given date and time
Sub SetDateAndTime(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int) As Long

'Returns the ticks value of the given date and time with the specified time zone offset.
'The last parameter is the time zone offset measured in hours.
Public Sub SetDateAndTime2(Years As Int, Months As Int, Days As Int, Hours As Int, Minutes As Int, Seconds As Int, _
      TimeZone As Double) As Long

'Returns the number of days in the given month
Sub NumberOfDaysInMonth(Month As Int, Year As Int) As Int

'Adds a Period to the given date instance. Do not forget to assign the result.
Sub AddPeriod(Ticks As Long, Per As Period) As Long

'Tests whether the two ticks values represent the same day.
Sub IsSameDay(Ticks1 As Long, Ticks2 As Long) As Boolean

'Converts ticks value to unix time.
Sub TicksToUnixTime(Ticks As Long) As Long

'Converts unix time to ticks value.
Sub UnixTimeToTicks(UnixTime As Long) As Long

DateUtils includes a type named Period:
B4X:
Type Period (Years As Int, Months As Int, _
      Days As Int, Hours As Int, Minutes As Int, Seconds As Int)

You can use PeriodBetween method to get the Period between two dates.
AddPeriod method adds a Period to a given date instance.

The DateUtils module is included in the attached example.

v1.05: Adds SetDateAndTime2. Allows you to explicitly set the time zone.

v1.03: Fixes a bug in NumberOfDaysInMonth method.

v1.02: Fixes a locale related bug (SetDate and SetDateAndTime).

v1.01: Adds PeriodBetweenInDays - Similar to PeriodBetween however the years and months will be zero (days field will be larger as needed).
This version also fixes a bug in SetDateAndTime related to DST

Starting from B4A v2.70, DateUtils is included as a library in the IDE.
 
Last edited:

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
 
  • Like
Reactions: omo

fotosettore

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

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 !!!!!!!
 
Top