# DateTime: No week 1 in 2021?

**URL:** <https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088>\
**Category:** General\
**Created:** [December 28, 2020, 11:30pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088 "2020-12-28T23:30:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [December 28, 2020, 11:30pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/1 "2020-12-28T23:30:11Z")

</div>

I have a web app (API 1.0, so Xojo 2019r3.2) with a custom calendar control based on DateTime (Yes, I know a plug in would be much wiser in terms of performance. For reasons unknown to me by customer request).

I was just made aware that there is no week 1 when you cross next new year.  
Could it be that DateTime gets WeekOfYear wrong?

The quick and dirty code that builds my date time array:

```
Var firstDayOfMonth As DateTime = today.firstDayOfMonth
Var FirstWeekDay As Integer = firstDayOfMonth.GermanDayOfWeek
Var LeadingMonthDays As Integer = FirstWeekDay-1
Var lastDayOfMonth As DateTime = Today.LastDayOfMonth
Var LastWeekDay As Integer = lastDayOfMonth.GermanDayOfWeek
Var TrailingMonthDays As Integer = 7 - LastWeekDay
DisplayDays.ResizeTo(LeadingMonthDays-1)
Var currentdatadate As New DateTime(firstDayOfMonth)
For q = LeadingMonthDays -1 DownTo 0
  currentdatadate = currentdatadate-New DateInterval(0, 0, 1)
  DisplayDays(q) = New DateTime(currentdatadate)
Next
currentdatadate = New DateTime(firstDayOfMonth)
DisplayDays.AddRow New date(currentdatadate)
For q = 2 To lastDayOfMonth.Day
  currentdatadate = currentdatadate+ New DateInterval(0,0,1)
  DisplayDays.AddRow New date(currentdatadate)
Next
For q = DisplayDays.LastRowIndex To 40
  currentdatadate = currentdatadate+ New DateInterval(0,0,1)
  DisplayDays.AddRow New date(currentdatadate)
Next

```

(So basically I just build a days array, starting with Monday of the first or leading week, adding a timeinterval of one day for each item which should be safe I guess.)  
GermanDayOfWeek simply shifts dayOfWeek by one because our weeks start with Monday.  
And part of the control’s paint event:

```
If ShowWeekNumbers Then // Wochennummern
  Displaywidth = Displaywidth - 35
  LeftBorder = 36
  g.DrawLine 35,TopBorder,35,g.Height
  For rows As Integer = 0 To If (Me.ViewMode = ViewModes.Monthly, 6, 0)
    g.DrawLine 1,TopBorder+rows*cellheight, 36, topborder+rows*cellheight
    If rows < 6 Then
      Var currentdate As DateTime = DisplayDays(rows*7)
      Var week As Integer = currentdate.WeekOfYear
      g.TextFont = "Noto Sans"
      g.TextSize = 12
      g.DrawString week.ToString("00"), 8, TopBorder+rows*Cellheight+17
    End If
  Next
End If

```

That bug is not based on crossing the year. February 2021 starts with week 6 which is wrong.  
Is this a known bug? Was it fixed?  
(I will have to resort to a workaround anyway and think that checking for an offset – if Jan 1st of the current year is not in week one, subtract 1 – will be sufficient, but I blushed a bit. Or any mistake on my side visible?)

 ![Capto_Capture 2020-12-29_12-15-17_AM](https://forum.xojo.com/uploads/default/original/2X/5/502cfa540afd195cf471b554ab2fcfc689567c92.png)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [December 28, 2020, 11:49pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/2 "2020-12-28T23:49:36Z")

</div>

From what I understand January 1st will always be week 1 but January 2nd could be the start of week 2. From the [docs](https://documentation.xojo.com/api/data_types/datetime.html#datetime-weekofyear):

> ## Notes
> 
> The first week is numbered 1. The first week may be incomplete. If January 1 falls on a Saturday, then the next day is in week 2.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [December 28, 2020, 11:57pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/3 "2020-12-28T23:57:27Z")

</div>

That would be logical in mathematical terms, but not in calendaric ones – at least not for Germany.  
For us, week one starts on January 4th.

> **[Alle Kalenderwochen (KW) des laufenden Jahres 📅](https://www.smart-rechner.de/kalenderwochen/rechner.php)**

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [December 29, 2020, 12:00am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/4 "2020-12-29T00:00:15Z")

</div>

After some more research: The first week of the year is that one with the first Thursday in it.  
So WeekOfYear is quite misleading in terms of ISO-compatible calendar calculations. But can be easily fixed.  
(And, as you said, ok by the docs. But not for more internationally.)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [December 29, 2020, 12:01am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/5 "2020-12-29T00:01:34Z")

</div>

I can’t help you with that, I have never seen Week of Year being used in Mexico so I don’t know if there is an international standard on how to use it.

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 8:22am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/6 "2021-01-07T08:22:10Z")

</div>

I just battled the topic of ISO week numbers where Monday is being recognized as 1st day of Week and where 1st Thursday in January of a given year declares the whole week as a first one and so on… it uses old API but works 😉

Public Function ISOWeekNumber(dt as date) as integer  
// nr of week for given date  
// as based od ISO 8601 date and time standard  
// where Monday is 1st day of week

var dow2,dow,woy,woy2 as integer  
dow=dt.DayOfWeek-1  
if dow=0 then dow=7  
woy=Floor((10+dt.DayOfYear-dow)/7)  
if woy=0 then  
var dp as new date (dt.Year-1,12,31)  
dow2=dp.DayOfWeek-1  
if dow2=0 then dow2=7  
woy=Floor((10+dp.DayOfYear-dow2)/7)  
else  
if woy=53 then  
var dp as new date (dt.Year+1,1,1)  
dow2=dp.DayOfWeek-1  
if dow2=0 then dow2=7  
if dow2\<5 then woy=1  
end if  
end if  
Return woy

End Function

---

<div class="post-metadata">

**Author:** ![Horst\_Jehle](https://forum.xojo.com/user_avatar/forum.xojo.com/horst_jehle/32/5432_2.png) [@Horst\_Jehle](https://forum.xojo.com/u/Horst_Jehle)\
**Post date:** [January 7, 2021, 10:11am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/7 "2021-01-07T10:11:51Z")

</div>

In my application [KW13](https://itunes.apple.com/de/app/cw13/id866584603?l=de&ls=1&mt=12), I use “ncal -w 1 2021” to get the calendar week numbers.

ncal -w 1 2021  
Januar 2021  
Mo 4 11 18 25  
Di 5 12 19 26  
Mi 6 13 20 27  
Do 7 14 21 28  
Fr 1 8 15 22 29  
Sa 2 9 16 23 30  
So 3 10 17 24 31  
KW 53 1 2 3 4

 ![KW13](https://forum.xojo.com/uploads/default/original/2X/9/9c16453433cc416b7b7d9bbaf3ba50d35fd1b7ac.jpeg)

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 10:44am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/8 "2021-01-07T10:44:12Z")

</div>

I knew about ncal under MacOS but what about Windows…? hence I did it on my own.

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 11:03am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/9 "2021-01-07T11:03:16Z")

</div>

…there are more problems:

- the last days of previous year can be in 1st week
- or, first days of actual year can be in last week of previous year.

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 7, 2021, 11:07am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/10 "2021-01-07T11:07:20Z")

</div>

Is this an OS bug or a Xojo one?  
If the OS always uses the same rules regardless of the chosen locale settings, the only doable fix is in code (which is just a workaround).  
But if the OS has an API to return the correct value (which Xojo doesn’t use) or otherwise a bug in Xojo, then it should just be fixed there and the standard method would work.

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 11:11am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/11 "2021-01-07T11:11:31Z")

</div>

XOJO uses US approach - 1st day of January declares the whole week as 1st week of year - and Sunday is first day of a week. Easy. Then comes ISO norm… such week can be addressed the 1st week of actual year and - in the same time, the last week for previous year. So for 2020-12-31 it can be 53rd week and at the same time, 2021-01-01 can be 1st week. 53rd and 1st points to the same week which is avoidable using ISO approach.

---

<div class="post-metadata">

**Author:** ![Jeannot\_Muller](https://forum.xojo.com/user_avatar/forum.xojo.com/jeannot_muller/32/13359_2.png) [@Jeannot\_Muller](https://forum.xojo.com/u/Jeannot_Muller)\
**Post date:** [January 7, 2021, 11:11am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/12 "2021-01-07T11:11:41Z")

</div>

> [@Pawel\_Soltysinski](#):
>
> it uses old API but works 😉

What do you mean by this? Your code seems to work perfectly fine 💪 if we replace date by datetime, at least as far as I could see in a quick test.

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 11:12am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/13 "2021-01-07T11:12:21Z")

</div>

sure, it could be datetime instead of date 😃

---

<div class="post-metadata">

**Author:** ![Horst\_Jehle](https://forum.xojo.com/user_avatar/forum.xojo.com/horst_jehle/32/5432_2.png) [@Horst\_Jehle](https://forum.xojo.com/u/Horst_Jehle)\
**Post date:** [January 7, 2021, 11:18am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/14 "2021-01-07T11:18:12Z")

</div>

If you use this, the week number is always 2.

var dd as DateTime  
dd = DateTime.now(New TimeZone(“Europe/Berlin”))

Normally, I would expect that specifying a time zone would also take the correct values for the calendar week.

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [January 7, 2021, 11:18am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/15 "2021-01-07T11:18:52Z")

</div>

> [@Pawel\_Soltysinski](#):
>
> XOJO uses US approach - 1st day of January declares the whole week as 1st week of year - and Sunday is first day of a week. Easy. Then comes ISO norm…

For all of us outside of the US, it’s the opposite… We have our usual everyday rules. Then comes US norm which Xojo uses.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [January 7, 2021, 11:21am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/16 "2021-01-07T11:21:23Z")

</div>

Don’t think it’s used here in the UK either.

---

<div class="post-metadata">

**Author:** ![Pawel\_Soltysinski](https://forum.xojo.com/user_avatar/forum.xojo.com/pawel_soltysinski/32/23522_2.png) [@Pawel\_Soltysinski](https://forum.xojo.com/u/Pawel_Soltysinski)\
**Post date:** [January 7, 2021, 11:24am UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/17 "2021-01-07T11:24:40Z")

</div>

normally you don’t pay attention to week numbers but when it comes to i.e. production planning, to synchronize deliveries of raw materials etc. in EU - they all use week numbers to confirm 😕

---

<div class="post-metadata">

**Author:** ![Jeannot\_Muller](https://forum.xojo.com/user_avatar/forum.xojo.com/jeannot_muller/32/13359_2.png) [@Jeannot\_Muller](https://forum.xojo.com/u/Jeannot_Muller)\
**Post date:** [January 7, 2021, 12:26pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/18 "2021-01-07T12:26:33Z")

</div>

same for financial reporting and forecasting (at least in the EU). I’m wondering what MS is using in Outlook. Just ISO worldwide?

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [January 7, 2021, 12:33pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/19 "2021-01-07T12:33:35Z")

</div>

Yes, I did hear it in some meetings with European colleagues before I retired. But before that, I’d never heard of the idea at all, neither at CERN, nor while working 12 years in the USA.

---

<div class="post-metadata">

**Author:** ![Jeannot\_Muller](https://forum.xojo.com/user_avatar/forum.xojo.com/jeannot_muller/32/13359_2.png) [@Jeannot\_Muller](https://forum.xojo.com/u/Jeannot_Muller)\
**Post date:** [January 7, 2021, 2:11pm UTC](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088/20 "2021-01-07T14:11:28Z")

</div>

In Germany it is a big thing in midsize to larger organisation. You don’t meet in 2 weeks or in 4 months, but you want to meet in “Kalenderwoche 34” or “Kalenderwoche 48” and you are on vacation in calendar week xy etc. Honestly, I think most people don’t care when the first weeks of a year starts, they trust their outlook calendar and that is showing, and that seems to be ISO (at least with the regional timezone settings for Germany).

[Next page](https://forum.xojo.com/t/datetime-no-week-1-in-2021/60088.md?page=2)
