Week number

volvomann

Active Member
Licensed User
Longtime User
Hello!
Is there any possibility to get the week number from date time or anything else, if possible, how can I do it. Helsing volvoman
 

volvomann

Active Member
Licensed User
Longtime User
Week day

Hey. it works but it will be a week for small, we are in week 11 but it shows only week 10, someone who has a good explanation
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How about this?
Dim WeekNum As Int
WeekNum = Ceil(DateTime.GetDayofYear(DateTime.Now)/7)
Msgbox(WeekNum,"") 'returns 11
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
Calculating the week number isn't that easy because the first week of the year is the week that contains that year's first Thursday.

This is the code I'm using at the moment (until I'll find a better solution):
B4X:
Label1.Text = "Current weeknumber: " & GetWeekNumber(DateTime.Now)

...

Sub GetWeekNumber(ticks As Long) As Int
   Dim currentThursday, firstThursday As Long
   currentThursday = GetThursdayOfWeekInTicks(ticks)
   firstThursday = GetThursdayOfWeekInTicks(DateTime.DateParse("01/04/" & (DateTime.GetYear(currentThursday))))
   Return Floor((currentThursday - firstThursday) / DateTime.TicksPerDay / 7 + 1)
End Sub

Sub GetThursdayOfWeekInTicks(ticks As Long) As Long
   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
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Specci48: Can one use your same code every year or does one have to modify it every year to accommodate the proper date?
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
The above code works for every year.
You should only be aware of correcting the DateParse string if you use a different DateFormat.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thank you. I am assuming you are using M/D/yyyy format. I still do not understand the significance of the specific date in the code of "01/04". In other words, why did you pick 1/4?
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Why don't make it simple?

B4X:
DateTime.DateFormat = "w"
Log(DateTime.Date(DateTime.Now))
 
Last edited:
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
I use (dd / mm / yy) what should I use instead of "01/04 /"? I get to week -1 if I use 0! / 04 I tried to insert dd / mm / YYYY but then I get errors
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
The aim is to DateParse January, 4th of the current year.
It might be helpful to save the current DateFormat before using the DateParse statement and put it back to the original value afterwards.

So
B4X:
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
instead an everything should be fine.


specci48
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Yes yes thank you i works! Butt shoud not diff ben set as int or somting, it is red in my IDE but nor error.
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
Why don't make it simple?

B4X:
DateTime.DateFormat = "w"
Log(DateTime.Date(DateTime.Now))

Thank you corwin42 for that valuable tip. :sign0142:
I took my code from an old basic4ppc program and I'm not sure if .net has this format available.
Important difference: The above DateFomat = "w" assumes that the week begins with sunday. Here in germany the first day of the week is monday. So I still have to put some code around it...

specci48
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Important difference: The above DateFomat = "w" assumes that the week begins with sunday. Here in germany the first day of the week is monday. So I still have to put some code around it...
Thats because the internal DateTime objects uses the Locale_US locale hardcoded. You can Use AHLocale DateTime object then it should be always correct.

B4X:
Dim dt As AHDateTime
   
dt.Initialize
dt.Pattern = "w"
weeknr = dt.format(DateTime.Now)

This will use the device locale and should be correct everywhere in the world.
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Here is a copy from IDE ( all keywords are blue)

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
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
 
Upvote 0
Top