Italian ♫ ♫ ho girato e rigirato, senza trovare nulla ♫♫♫ giorno dell'anno

giannimaione

Well-Known Member
Licensed User
Longtime User
[B4J] [B4A] Conoscendo la data, come calcolare il numero del giorno e il numero della settimana?
esempio: data 18/09/2023 -> 261 giorno e 38 settimana
 

MarcoRome

Expert
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    'MainForm.Show
    
    Dim t As Long = DateTime.DateParse("09/18/2023") + DateTime.TicksPerDay
    Log($"$Date{t}: Week #${GetWeekNumberStartingFromMonday(t)}, Day: ${getNumberDate(t)}"$)
End Sub

Sub GetWeekNumberStartingFromMonday (ticks As Long) As Int
    Dim WeekDayOfFirstDayOfYear As Int = DateTime.GetDayOfWeek(DateUtils.SetDate(DateTime.GetYear(ticks), 1, 1)) - 1
    Dim FirstMondayInYear As Int = (7 + WeekDayOfFirstDayOfYear - 1) Mod 7
    Dim result As Int
    If WeekDayOfFirstDayOfYear <> 1 Then result = result - 1
    result = result + Floor((DateTime.GetDayOfYear(ticks) - 1 + FirstMondayInYear) / 7) + 1
    Return result
End Sub

Sub getNumberDate (datet As Long)  As Int
    DateTime.DateFormat = "yyyy/MM/dd"
    Dim yr As Int = DateTime.GetYear(DateTime.Now)
    Dim p As Period = DateUtils.PeriodBetweenInDays(DateTime.DateParse($"${yr}/01/01"$), datet)
    Return p.Days
End Sub

La settimana presa da un esempio di Erel:

 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…