Android Question Fortnight Calculation

BedDweller

Member
Licensed User
Longtime User
I've looked though the DateTime and DateUtils trying to find a way to calculate weather or not week X is a odd or even week (starting with the week Wednesday 1/1/2014 being odd, then Sunday 5/1/2014 being the start of a even week)...

I've thought about going on the lines of
B4X:
If DateTime.GetWeekofYear Mod 2 = 0 Then
    Week = Even
Else
    Week = Odd
End If
but there is no GetWeekofYear function, and using a series of functions alone that line to get that number seems prone to bugs... and as for playing with the Ticks, I been kinda daunted form playing with them as there's a lot of 'how to use', but can't really find, 'this is what a tick is'.
Thanks
 

BedDweller

Member
Licensed User
Longtime User
looks like I just needed to clear my head... anyway answer for future use for anyone who comes along this problem,
B4X:
((DateTime.GetDayOfYear(DateTime.Now)+(DateTime.GetDayOfWeek(DateTime.DateParse("01/01/"&DateTime.GetYear(DateTime.Now)))))/7+1)
gives the Week of Year.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is easier to use "w" as formatsetting. And, btw, much less code needed
B4X:
DateTime.Dateformat = "w"
Log("Week="&DateTime.Date(DateTime.now))

or you can write (okok, i already did :D) a sub for it and use this sub.

B4X:
Sub GetWeekOfYear(dt As Long) As Int
    ' store old timeformat pattern
    Dim oldsetting As String = DateTime.DateFormat

    ' set temporary timeformat pattern
    DateTime.DateFormat = "w"

   ' Get weekno
    Dim weekno As Int
    weekno = DateTime.Date(dt)

    ' restore old timeformat pattern
    DateTime.DateFormat = oldsetting

    ' return weekno as int
    Return weekno
End Sub

B4X:
Log("WeekNo = "&GetWeekOfYear(DateTime.now))
 
Last edited:
Upvote 0

BedDweller

Member
Licensed User
Longtime User
It is easier to use "w" as formatsetting. And, btw, much less code needed
B4X:
DateTime.Dateformat = "w"
Log("Week="&DateTime.Date(DateTime.now))

or you can write (okok, i already did :D) a sub for it and use this sub.

B4X:
Sub GetWeekOfYear(dt As Long) As Int
    ' store old timeformat pattern
    Dim oldsetting As String = DateTime.TimeFormat

    ' set temporary timeformat pattern
    DateTime.TimeFormat = "w"

   ' Get weekno
    Dim weekno As Int
    weekno = DateTime.Date(dt)

    ' restore old timeformat pattern
    DateTime.TimeFormat = oldsetting

    ' return weekno as int
    Return weekno
End Sub

B4X:
Log("WeekNo = "&GetWeekOfYear(DateTime.now))

Almost works like a Charm, but it throw a "Not a Valid Double" error... you might want to correct your code to use Date.Time.DATEFormat... after fixing that it works perfectly ^.^ thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Almost works like a Charm, but it throw a "Not a Valid Double" error... you might want to correct your code to use Date.Time.DATEFormat... after fixing that it works perfectly ^.^ thanks

Inside the sub, yes, you are right... I´ve changed code above thank you
 
Upvote 0
Top