weeknumber trouble

volvomann

Active Member
Licensed User
Longtime User
I use this code in my apps for find weeknr. It works ok but between 00 and 1.00 the weeknumber is weeknumber -1 and after 1.00 its right again, can enyone help me to fiks that?

B4X:
Sub Process_Globals
   
Dim dt As AHDateTime
   dt.Initialize
   dt.Pattern = "w"
End Sub
Sub GetWeekNumber(ticks As Long) As Int
   Dim currentThursday, firstThursday As Long
   Dim savedDateFormat As String
   currentThursday = GetThursdayOfWeekInTicks(ticks)
   savedDateFormat = DateTime.DateFormat
   DateTime.DateFormat = "MM/dd/yyyy"
   firstThursday = GetThursdayOfWeekInTicks(DateTime.DateParse("01/04/" & (DateTime.GetYear(currentThursday))))
   DateTime.DateFormat = savedDateFormat
   Return Floor((currentThursday - firstThursday) / DateTime.TicksPerDay / 7 + 1)
End Sub

Sub GetThursdayOfWeekInTicks(ticks As Long) As Long
   Dim diff As Int
   Select DateTime.GetDayOfWeek(ticks)
      ' Sunday
      Case 1
         diff = -3   
      ' Monday
      Case 2
         diff = 3
      ' Tuesdy
      Case 3
         diff = 2
      ' Wednesday
      Case 4
         diff = 1
      ' Thursday
      Case 5
         diff = 0
      ' Friday
      Case 6
         diff = -1
      ' Saturday
      Case 7
         diff = -2      
   End Select   
   Return DateTime.Add(ticks, 0, 0, diff)
End Sub
 

specci48

Well-Known Member
Licensed User
Longtime User
Hello volvomann, remembering this thread?

The short solution by corwin42 is:
B4X:
Dim dt As AHDateTime
dt.Initialize
dt.Pattern = "w"
weeknr = dt.format(DateTime.Now)

specci48
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User

Yes i rember that i mean i use this code only that i have code in a module.
Or have i missundertood.
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
The four lines in post#2 replace all of your lines in post#1.

As I mentioned in that other thread, I used the complex way to calculate the weeknumber until I know a better way. And corwin32 provided this better way so there is no need to use my old (and longer) solution.


specci48
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User

I thing i use the longway that givs the weeknummer rigth unless from 00 1.00 the shortway giws wrong weeknummer +1 thank you fro the help
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Can you explain this? What is between 00 and 1.00?
Sorry but I didn't get i yet :sign0013:

specci48

When the clock is between 00 and 01.00 at night the weekno goes from the right weekno. to weekno -1 afther 01.00 the weekno is corect again. You understand what i mean?
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
It sounds like a time zone problem, which time zone are you in and are you using the emulator.
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
It sounds like a time zone problem, which time zone are you in and are you using the emulator.

GMT+02:00 Central European Summertime I have the same "trouble" on my phone there i use automaticly date and time from the net. time and date
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…