Android Question Looking for the first date of a weeknr in the past

parijs

Active Member
Licensed User
example:

I pick a date in the calendar May 2 2014 (week 21)

Now I want to know the date of the Monday and Friday of this week
 

derez

Expert
Licensed User
B4X:
DateTime.DateFormat = "dd/MM/yyyy"
Dim seldate As Long = DateTime.DateParse("02/05/2014")
Dim n As Int = DateTime.GetDayOfWeek(seldate)
Dim Mt As Long = seldate - (n-2)* DateTime.TicksPerDay
Dim Ft As Long = seldate - (n-6)* DateTime.TicksPerDay
Log("Monday = " & DateTime.Date(Mt) & " Friday = " & DateTime.Date(Ft))
 

Erel

Administrator
Staff member
Licensed User
It is better to use DateUtils.AddPeriod:
B4X:
DateTime.DateFormat = "dd/MM/yyyy"
Dim seldate As Long = DateTime.DateParse("02/05/2014")
Dim n As Int = DateTime.GetDayOfWeek(seldate)
Dim p1 As Period
p1.Days = 2 - n
Dim Mt As Long = DateUtils.AddPeriod(seldate, p1)
p1.Days = 6 - n
Dim Ft As Long = DateUtils.AddPeriod(seldate, p1)
Log("Monday = " & DateTime.Date(Mt) & " Friday = " & DateTime.Date(Ft))
 

derez

Expert
Licensed User
How about what you use in DateUtils, so you don't need the class at all:
B4X:
Dim Mt As Long = DateTime.Add(seldate,0,0,2-n)
Dim Ft As Long = DateTime.Add(seldate,0,0,6-n)
 
Top