Android Code Snippet [B4X] Get number of days in year

B4X:
Public Sub GetNumberOfDaysInYear(Year As Int) As Int
    Return DateUtils.PeriodBetweenInDays(DateUtils.SetDate(Year,1,1),DateUtils.SetDate(Year,12,DateUtils.NumberOfDaysInMonth(12,Year))).Days +1
End Sub
or
B4X:
Public Sub GetNumberOfDaysInYear2(Year As Int) As Int
    Return DateTime.GetDayOfYear(DateUtils.SetDate(Year,12,31))
End Sub
or
from @mcqueccu
B4X:
Public Sub GetNumberOfDaysInYear3(Year As Int) As Int
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim d As Long = DateTime.DateParse($"${Year}/12/31"$)
    Return DateTime.GetDayOfYear(d)
End Sub
or
from @epiCode
B4X:
Sub GetDaysInYear(year As Int) As Int
    Return IIf((year Mod 4 = 0 And year Mod 100 <> 0) Or year Mod 400 = 0, 366, 365)
End Sub
 
Last edited:

mcqueccu

Well-Known Member
Licensed User
Longtime User
When I run for the year 2023 I got 364.

I was thinking i could just parse last day of the year example 31-12-2023 as long to this method DateTime.GetDayOfYear(). But it gives 365.

It
 

Theera

Well-Known Member
Licensed User
Longtime User
B4X:
Public Sub GetNumberOfDaysInYear(Year As Int) As Int
    Return DateUtils.PeriodBetweenInDays(DateUtils.SetDate(Year,1,1),DateUtils.SetDate(Year,12,DateUtils.NumberOfDaysInMonth(12,Year))).Days +1
End Sub
or
B4X:
Public Sub GetNumberOfDaysInYear2(Year As Int) As Int
    Return DateTime.GetDayOfYear(DateUtils.SetDate(Year,12,31))
End Sub
or
from @mcqueccu
B4X:
Public Sub GetNumberOfDaysInYear3(Year As Int) As Int
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim d As Long = DateTime.DateParse($"${Year}/12/31"$)
    Return DateTime.GetDayOfYear(d)
End Sub
or
from @epiCode
B4X:
Sub GetDaysInYear(year As Int) As Int
    Return Iff((year Mod 4 = 0 And year Mod 100 <> 0) Or year Mod 400 = 0, 366, 365)
End Sub
Let me include your class in B4xstringfunctionVB.
B4X:
Public Sub GetNumberOfDaysInYear(Year As Int) As Int
    Return DateUtils.PeriodBetweenInDays(DateUtils.SetDate(Year,1,1),DateUtils.SetDate(Year,12,DateUtils.NumberOfDaysInMonth(12,Year))).Days +1
End Sub
or
B4X:
Public Sub GetNumberOfDaysInYear2(Year As Int) As Int
    Return DateTime.GetDayOfYear(DateUtils.SetDate(Year,12,31))
End Sub
or
from @mcqueccu
B4X:
Public Sub GetNumberOfDaysInYear3(Year As Int) As Int
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim d As Long = DateTime.DateParse($"${Year}/12/31"$)
    Return DateTime.GetDayOfYear(d)
End Sub
or
from @epiCode
B4X:
Sub GetDaysInYear(year As Int) As Int
    Return Iff((year Mod 4 = 0 And year Mod 100 <> 0) Or year Mod 400 = 0, 366, 365)
End Sub
Let me include your class in B4xstringfunctionVB.
 
Top