# System.date.format() returns wrong calendar week

**URL:** <https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 20, 2022, 1:22pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828 "2022-12-20T13:22:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [December 20, 2022, 1:22pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/1 "2022-12-20T13:22:54Z")

</div>

Hi there,  
i am calculating the calendar week from a timestamp in a property transform script. The script is not returning what I would expect.

Here is the code:

```auto
timestamp = 1671540467576
date = system.date.fromMillis(timestamp)
calendar_week = system.date.format(date, 'w')

```

In the transform script this returns **calendar\_week = 52**

If I test the code in the script editor I get what I would expect: **calendar\_week = 51**

Any ideas, why the results are different and what causes the transform script to return week 52?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 20, 2022, 1:45pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/2 "2022-12-20T13:45:23Z")

</div>

I wonder if it's an integer casting issue, where the expected value is being rounded up.

---

<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:** [December 20, 2022, 1:52pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/3 "2022-12-20T13:52:40Z")

</div>

Are your designer and gateway in the same locale?

The designer script console runs in the Client scope, so whatever local that your machine is set to.

Script transforms run in gateway scope so whatever local the gateway machine is set to.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 20, 2022, 1:55pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/4 "2022-12-20T13:55:32Z")

</div>

The [8.1 documentation](https://docs.inductiveautomation.com/display/DOC81/system.date.format#:~:text=The%20default%20is%20%22yyyy%2DMM,string%20representing%20the%20formatted%20datetime.&text=%23%20This%20is%20a%20standard%20format%20that%20all%20databases%20recognize.) is somewhat vague, but the [8.0 documentation](https://docs.inductiveautomation.com/display/DOC80/system.date.format) says this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/b/db7d506db478e1cd689df5c585286d9c2b7b9adb.png)

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [December 20, 2022, 2:22pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/6 "2022-12-20T14:22:21Z")

</div>

I found that bit as well, but I understood it as only the 31st of December would fall into week 1.

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [December 20, 2022, 2:25pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/7 "2022-12-20T14:25:01Z")

</div>

If I change the formatting e.g. to 'dd.MM.yyyy', I get the correct date.

```auto
	timestamp = 1671540467576
	date = system.date.fromMillis(timestamp)
	formated_date = system.date.format(date, 'dd.MM.yyy')

```

returns → 20.12.2022

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 20, 2022, 2:32pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/8 "2022-12-20T14:32:56Z")

</div>

This is the underlying implementation:

[https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html)

Also read the package introduction, where it points out that all base classes use ISO definitions:

[https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html)

If you need more, you can always read the java source code (via OpenJDK).

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [December 20, 2022, 2:51pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/9 "2022-12-20T14:51:24Z")

</div>

If I understand this correctly the ISO week-of-week-based-year should be 51.

python datetime functions return calendar week = 51 in script console as well as in the transform script:

```auto
from datetime import datetime
timestamp = 1671540467576
py_date = datetime.fromtimestamp(timestamp/1000)
cw = py_date.strftime('%U')

```

So that could be a workaround. But i try to stick to @pturmel advice to avoide datetime functions. 🤔

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 20, 2022, 2:56pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/10 "2022-12-20T14:56:21Z")

</div>

This seems to work as well:

```auto
timestamp = 1671540467576
date = system.date.fromMillis(timestamp)
calendar_week = system.date.format(date, 'w')
calendar_day = system.date.format(date, 'D')

print calendar_week
print float(calendar_day)/7.0
print round(float(calendar_day)/7.0)
print int(round(float(calendar_day)/7.0))

```

**Output:**

```auto
52
50.5714285714
51.0
51

```

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [December 21, 2022, 7:01pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/11 "2022-12-21T19:01:24Z")

</div>

@lrose: You were correct. The locale setting is causing the difference between designer and script transform.  
My designer was running with locale de\_DE and the Gateway is running wit en\_US. I did not manage to change the Gateways locale, which is running in docker, but after changing my client to en\_US the system.date.format(date,'w') returned week 52 in the designer as well.  
I still don't understand, why en\_US would have another calendar\_week as locale C.UTF-8 or de\_DE.UTF-8.  
If someone got some documentation how the various locales are affecting week\_of\_yearcalculation, that would be nice.

---

<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:** [December 22, 2022, 3:05am UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/12 "2022-12-22T03:05:20Z")

</div>

Well if you think about it, I think you can probably work it out. _Very_ basically the local affects the time zone offset of the date, which in some circumstances can cause Week 1 to shift.

This has to do with the way that week 1 is calculated, by the ISO definition. Due to the time zone offset it is possible for Jan 1 to be on a Sunday in some time zones and Monday in others.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 22, 2022, 1:33pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/13 "2022-12-22T13:33:16Z")

</div>

> [@lrose](#):
>
> Due to the time zone offset it is possible for Jan 1 to be on a Sunday in some time zones and Monday in others.

Huh? I think not.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 22, 2022, 2:48pm UTC](https://forum.inductiveautomation.com/t/system-date-format-returns-wrong-calendar-week/67828/14 "2022-12-22T14:48:35Z")

</div>

More like the locale can affect where week 1 begins.

You can build a formatter using java.time to just make it ISO, regardless of locale.

```python
from java.time import Instant, ZoneOffset
from java.time.temporal import WeekFields
from java.time.format import DateTimeFormatterBuilder

timestamp = 1671540467576

print '--- US Locale ---'
date = system.date.fromMillis(timestamp)
print ' Date:', date
print 'WeekOfYear:', system.date.format(date, 'w')
print ' '

print '--- ISO ---'
formatter = DateTimeFormatterBuilder() \
				.appendValue(WeekFields.ISO.weekOfWeekBasedYear(), 2) \
				.toFormatter().withZone(ZoneOffset.UTC)

date = Instant.ofEpochMilli(timestamp)
print ' Date:', date
print 'WeekOfYear:', formatter.format(date)

```

Output:

```nohighlight
--- US Locale ---
      Date: Tue Dec 20 07:47:47 EST 2022
WeekOfYear: 52
 
--- ISO ---
      Date: 2022-12-20T12:47:47.576Z
WeekOfYear: 51

```
