change WeekOfYear

Hi,

one question: Date.WeekOfYear is readonly.

Does anyone knows a way how to implement a function where I can SET a weekofyear property to get the actual date?

How do you plan to get a date from a whole week? Date’s in Xojo are actually specific datestamps. If you were to set a WeekOfYear, you’d be setting a whole week’s range, which isn’t how the Date object in the classic framework operates.

Are you trying to get this timeframe?
What do you want to do with it?

There may be a solution that exists already, and some more detail would help :slight_smile:

You cannot get a single date for weekofyear, there are 7 days in that week number.

Now, off the top of my mind, I would get todays weekofyear add or subtract 7 days per weekofyear. You may add further logic if you want the Monday of the week, for example.

I know that. I would also set the year and the day-of-week parameter (which is, I know, also read only).

OK. So (if I read between the lines) you want to answer the question “what is the date of the third day of the 19th week of year 2037?”.

The same basic idea applies. It is just a bit more intricate. I don’t believe that either Date or Xojo.Core.Date can directly perform the logic you are seeking. The new date object may make dateadd and datediff a bit easier.

Lars, so you want something like:

  • weekofyear 31
  • dayofweek 2
  • year 2018
    and get 30/july/2018?

So if you start with

.Day = 1
.Month = 1
.Year = 2018

Then add * 7 days to that, you have
Then add days to that.

If “Day 1 of week 1” is not 1 Jan, you need to start on instead.

Ah I had the same thoughts. This is my solution for this:

[code]Public Function getDateFromWeekOfYear(DayOfWeek as integer, WeekOfYear as integer, Year as Integer) as Date
dim d as new date
d.Year = Year
d.Day = 1
d.Month = 1

d.Day = d.Day * (WeekOfYear * 7)
d.Day = D.Day - (D.DayOfWeek - DayOfWeek) + 1 //+ 1 cause first day should be monday, not sunday

return d
End Function[/code]

Thanks four input guys!

I think the problem is with the first week of year, it is not always 7 days, so you need to do more calculations.

I used your code for:

  • year 2022
  • WeekOfYear 2
  • DayOfWeek 2
    and got 2022-01-11 and that’s the 3rd week of the year (and Tuesday).