For small ranges your code is correct. However there are two mistakes which will break it with larger ranges:
1. The simple one is that you need to use PeriodBetweenInDays instead of PeriodBetween (as we are not interested in months).
2. There is another mistake in this code which is not really significant in this case however in other cases it can be more significant.
To prove it check your code (after you fix point number one) with this value: 24000000
It will not return the correct value. I tested it with timezone set to Israel timezone. Different timezones will behave differently. However you will be able to find incorrect values for all timezones with day light saving periods.
The correct code:
Sub Activity_Create(FirstTime As Boolean)
Log(IncorrectMinutesToNiceString(2400000))
Log(CorrectMinutesToNiceString(2400000))
End Sub
Sub IncorrectMinutesToNiceString(minutes As Int) As String
Dim p As Period = dateutils.PeriodBetweenInDays(0,minutes * DateTime.TicksPerMinute)
Return $"${p.Days} days, ${p.Hours} hours and ${p.minutes} minutes"$
End Sub
Sub CorrectMinutesToNiceString(minutes As Int) As String
Dim t As Long = minutes * DateTime.TicksPerMinute
Dim days, hours, minutes As Int
days = t / DateTime.TicksPerDay '(24 hours day)
hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
Return $"${days} days, ${hours} hours and ${minutes} minutes"$
End Sub
The explanation:
Dates are complicated.
There are two different concepts related to dates:
- Time instance - A specific point in time: January 1st, 2010 08:00 GMT, another example: 2 days after January 1st, 2010 08:00.
- Period - For example 1000 minutes. A period by itself is not tied to any specific time instance
It is a mistake to tie a period to any specific time instance unless this is what we want to do. Your code implicitly ties the period to 1/1/1970 00:00.