B4R Question Get day of week

Shay

Well-Known Member
Licensed User
Longtime User
How can I get day of week? (providing the date from GSM/GPRS module)
(with no internet access - using AT command to get the date)
 

derez

Expert
Licensed User
Longtime User
B4X:
Sub DayOfTheWeek As Int  ' Sunday = 1
    Private mx, mi, y, p  As Int
'    Private c As Int = 20  ' two first digits of the year like 2017
    If month < 3 Then y = 1999 + year  Else y = 2000 + year
    mx = (month+9)Mod(12) +1
    mi = Floor(2.6*mx-0.2)
    p = y/4
'    r = 5 'c/4
'    Return (day + mi  -2 *c + y + p + r)Mod 7
    Return (day + mi   + y + p )Mod 7 '-2*c+r = -2*20+ 5 = -35 mod 7 = 0
End Sub

For years after 2000.
 
Last edited:
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
This is the porting of my old Java applet, returns a number and log the day name(in Italian sorry). Work with every day since year 0!:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim anno As Long=2017' Anno da calcolare
    Dim mese As Long=12  'Mese da calcolare
    Dim giorno As Long=04 ' Giorno da calcolare
   
    '1) "Domenica"
    '2) "Lunedì"
    '3) "Martedì"
    '4) "Mercoledì"
    '5) "Giovedì"
    '6) "Venerdì"
    '7) "Sabato"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log (weekday)

End Sub

Sub weekday As Int
    ' Elenco variabili.
    Dim giotra As Long ' Giorni trascorsi dall'inizio dell'anno 0.
    Dim giotra1 As Long ' Giorni trascorsi dall'inizio dell'anno inserito
    Dim giotra2 As Int ' Giorni mancanti alla fine dell'anno inserito
    Dim gioset As Int ' Giorno della settimana: 1-7 (Domenica-Sabato).
    Dim giomes As Int ' Giorni trascorsi dall'inizio dell'anno all'inizio del mese.
    Dim annobis As Int ' Anno bisestile: 0-1 (No-Si).
   

    ' Calcolo dell'anno bisestile.
    If ((anno<1582 And (anno Mod 4)=0) Or (anno>1582) And (anno Mod 400)=0) Or       ((anno>1582 And (anno Mod 4)=0) And (anno Mod 100)<> 0) Then
        annobis=1
    End If
        ' Calcolo dei giorni trascorsi dall'inizio dell'anno all'inizio del mese.
    Select Case mese
            Case 1
                giomes=0
            Case 2
                giomes=31
            Case 3
                giomes=59
            Case 4
                giomes=90
            Case 5
                giomes=120
            Case 6
                giomes=151
            Case 7
                giomes=181
            Case 8
                giomes=212
            Case 9
                giomes=243
            Case 10
                giomes=273
            Case 11
                giomes=304
            Case 12
                giomes=334
    End Select
    ' Calcolo dei giorni trascorsi dall'inizio dell'anno 0.
       
        giotra=anno*365+giomes*1+giorno+1+Floor(anno/4)
        If mese<3 Then giotra=giotra-annobis
        If (anno>1582) Then giotra=giotra+2+Floor(anno/400)-Floor(anno/100)
        If ((anno==1582 And mese>10) Or (anno==1582 And mese==10 And giorno>4)) Then giotra=giotra-10
        ' ora abbiamo i giorni trascorsi dall'anno 0

        ' Calcolo del tipo di calendario.
        '    If (giotra>578103)  Then form.tipocal.value="Gregoriano"'
        'Else form.tipocal.value="Giuliano";

        ' Calcolo del giorno della settimana.
        gioset=(giotra Mod 7)+4
        If (gioset>7) Then gioset=gioset-7
        If gioset=1 Then Log("Domenica")
        If gioset=2 Then Log ("Lunedi")
        If gioset=3 Then Log ("Martedì")
        If gioset=4 Then Log ("Mercoledì")
        If gioset=5 Then Log ("Giovedì")
        If gioset=6 Then Log ("Venerdì")
        If gioset=7 Then Log ("Sabato")
        ' Calcolo dei giorni trascorsi dall'inizio dell'anno.
        '    giotra1=giomes*1+giorno
        '    If mese>2 Then giotra1=giotra1+annobis
        '    If anno==1582 And giotra>578103 Then giotra1=giotra1-10
        '    'form.giotra1.value=giotra1;
        '
        '    ' Calcolo dei giorni mancanti alla fine dell'anno.
        '    giotra2=365+annobis-giotra1
        '    If (anno==1582) Then giotra2=355-giotra1
        'form.giotra2.value=giotra2;
        Return gioset
End Sub
 
Upvote 0
Top