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:

Croïd

Active Member
Licensed User
Longtime User
sorry, but I'm stuck at this level!

addtime = DateUtils.AddPeriod(now, diff)
 

Attachments

  • add.zip
    7.3 KB · Views: 542

Croïd

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

Ah ! Ok thanks
 

chrjak

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

chrjak

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

Erel

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

tdocs2

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

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

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Erel.

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

Best regards.

Sandy
 

LucaMs

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

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


Have you tried B4X object browser developed by Vader?
 

tdocs2

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

Anser

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

LucaMs

Expert
Licensed User
Longtime 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


B4X:
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:
Top