# Script - get calendar week number (but start with Monday not Sunday)

**URL:** <https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [February 17, 2021, 12:45pm UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510 "2021-02-17T12:45:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michal.koblasa](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@michal.koblasa](https://forum.inductiveautomation.com/u/michal.koblasa)\
**Post date:** [February 17, 2021, 12:45pm UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/1 "2021-02-17T12:45:10Z")

</div>

Hello,  
i need some hint about system.date.format.  
I need to format date to string like: “dd. MM. YYYY/w”  
That **w** is calendar week. But when I use this function I get Calendar weeks starting by Sunday (so if the date is hits Sunday, KW number is wrong for me).

a) Do you know any elegant way how get KW starting by Monday (some settings or elegant code).  
b) There is option to use ifs and add +1 if this is Sunday (but this is plan B for me).

Thank you

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 17, 2021, 3:23pm UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/2 "2021-02-17T15:23:19Z")

</div>

Since system.date.format uses the java SimpleDateFormat and SimpleDateFormat uses the Locale to determine which day is the start of the week, I assume that this is based on the project’s locale?

Perhaps @paul-griffith or @cmallonee can speak more to this?

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [February 17, 2021, 3:30pm UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/3 "2021-02-17T15:30:29Z")

</div>

Java’s Calendar class allows you to set the first day of the week ([docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html)). If you’re comfortable importing classes from Java this would be the easiest way.

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [February 18, 2021, 10:24pm UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/4 "2021-02-18T22:24:27Z")

</div>

Look at the strftime method from date time.

```auto
import datetime
today = datetime.date.today()
year, week, day = today.isocalendar()

# adjust for the week starting at 0
weekStartSun = int(today.strftime("%U")) + 1
weekStartMon = int(today.strftime("%W")) + 1

print year, week, weekStartSun, weekStartMon

```

---

<div class="post-metadata">

**Author:** ![michal.koblasa](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@michal.koblasa](https://forum.inductiveautomation.com/u/michal.koblasa)\
**Post date:** [February 19, 2021, 10:42am UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/5 "2021-02-19T10:42:20Z")

</div>

Thank you,  
i reused your example.  
M.

---

<div class="post-metadata">

**Author:** ![petr.sasin](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@petr.sasin](https://forum.inductiveautomation.com/u/petr.sasin)\
**Post date:** [December 18, 2024, 9:45am UTC](https://forum.inductiveautomation.com/t/script-get-calendar-week-number-but-start-with-monday-not-sunday/43510/6 "2024-12-18T09:45:39Z")

</div>

Method strftime("%W") is not working corectly. weekStartMon = 53 for date 18th December 2024. Correct value should be 52 according to ISO ([see Wikipedia](https://en.wikipedia.org/wiki/ISO_week_date)).  
Use just isocalenda() method to get week number starting on Monday according to ISO/European standard. Week number 1 is week containing first Thursday in January.

```python
import datetime
today = datetime.date.today() # get actual date 
today = datetime.date(2024,12,18) # set date manualy
print today,type(today)

year, week, day = today.isocalendar()

print "ISO calendar year = ",year
print "ISO calendar week = ",week
print "ISO calendar day (1..7 = Mo..Su) = ",day

```
