# system.date.getDayOfMonth unexpected output

**URL:** <https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [February 4, 2025, 12:11am UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808 "2025-02-04T00:11:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [February 4, 2025, 12:11am UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/1 "2025-02-04T00:11:13Z")

</div>

I'm trying to check if a date is exactly one year apart, to the second - I wanted to include leap years, so I consulted the docs and found this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/0/002b376690dfe0105f9b148c737434c1c9cf1218.png)

However, I tried selecting the same exact date from a date picker but one in 2024 and one in 2025, and I would get a value of \> 0. So I decided to check each part of the date so that I can capture the case where the dates are exactly one year apart, to the second.

Here is what that looks like in the script console (don't judge):

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/2/12319a2faff9f4c56cef43436fb3814ca7b98acb.png)

> **the code**
>
> ```python
> startDate = system.date.parse('2024-01-01 00:00:00', 'YYYY-MM-dd HH:mm:ss')
> endDate = system.date.parse('2025-01-01 00:00:00', 'YYYY-MM-dd HH:mm:ss')
> 
> print system.date.yearsBetween(startDate, endDate)
> print system.date.getSecond(startDate) == system.date.getSecond(endDate)
> print system.date.getMinute(startDate) == system.date.getMinute(endDate)
> print system.date.getHour24(startDate) == system.date.getHour24(endDate)
> print 'about to print dayOfMonth...'
> print system.date.getDayOfMonth(startDate) 
> print system.date.getDayOfMonth(endDate)
> print 'just printed dayOfMonth'
> print system.date.getMonth(startDate) == system.date.getMonth(endDate)
> print system.date.yearsBetween(startDate, endDate) == 0
> 
> ```

Why do I get unexpected results for `getDayOfMonth`, when they both should be 1 - at least that's what it looks like it should be on the surface. If that's the expected behavior, why?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 4, 2025, 12:21am UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/2 "2025-02-04T00:21:43Z")

</div>

Time to play 'spot the difference':

```python
startDate = system.date.parse('2024-01-01 00:00:00', 'yyyy-MM-dd HH:mm:ss')
endDate = system.date.parse('2025-01-01 00:00:00', 'yyyy-MM-dd HH:mm:ss')

print system.date.yearsBetween(startDate, endDate)
print system.date.getSecond(startDate) == system.date.getSecond(endDate)
print system.date.getMinute(startDate) == system.date.getMinute(endDate)
print system.date.getHour24(startDate) == system.date.getHour24(endDate)
print 'about to print dayOfMonth...'
print system.date.getDayOfMonth(startDate) 
print system.date.getDayOfMonth(endDate)
print 'just printed dayOfMonth'
print system.date.getMonth(startDate) == system.date.getMonth(endDate)
print system.date.yearsBetween(startDate, endDate) == 0

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/6/163efe8506b595c7baabb40f6473068c050d5649.png)

`YYYY` in `system.date.parse` gives you the 'ISO week year', _not_ the expected calendar year - `yyyy` is what you actually want.

[![](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/6/f6d53453c99ff042095fb320f7567456bdd7c50d.jpeg "Why 2020 Started On December 30th") ](https://www.youtube.com/watch?v=D3jxx8Yyw1c)

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [February 4, 2025, 12:25am UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/3 "2025-02-04T00:25:04Z")

</div>

I was just about to comment that the format string was breaking this...

```python
startDate = system.date.parse('2024-01-01 00:00:00')
endDate = system.date.parse('2025-01-01 00:00:00')

print system.date.yearsBetween(startDate, endDate)
print system.date.getSecond(startDate) == system.date.getSecond(endDate)
print system.date.getMinute(startDate) == system.date.getMinute(endDate)
print system.date.getHour24(startDate) == system.date.getHour24(endDate)
print 'about to print dayOfMonth...'
print startDate
print endDate
print system.date.getDayOfMonth(startDate) 
print system.date.getDayOfMonth(endDate)
print 'just printed dayOfMonth'
print system.date.getMonth(startDate) == system.date.getMonth(endDate)
print system.date.yearsBetween(startDate, endDate) == 0

```

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [February 4, 2025, 4:04pm UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/4 "2025-02-04T16:04:25Z")

</div>

I can't believe I missed something so silly ☹ Thank you for the quick response!

The reason I was testing that monstrosity was because I'm trying to limit a user to only 1 year's worth of data BUT when I select Feb 1, 2024 12:00 AM and Feb 1, 2025 12:00 AM, I get `system.date.yearsBetween() = 1`, which is okay, but then I don't know if its exactly one year to the second, or 1 year 11 months, etc.

What would be the best way to check if two dates are at most exactly one year apart, taking into account leap years?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 4, 2025, 4:33pm UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/5 "2025-02-04T16:33:28Z")

</div>

😬

Datetime math is _complicated_.

ChatGPT suggested something like this, after some prodding:

```python
def isAtMostOneYearApart(date1, date2):
    # Ensure date1 is the earlier date
    if date1.after(date2):
        date1, date2 = date2, date1

    # Get the number of whole years apart
    years_diff = system.date.yearsBetween(date1, date2)

    # If within the same year, return True
    if years_diff == 0:
        return True

    # If exactly one year apart, check if the days difference is within a year span
    if years_diff == 1:
        return system.date.daysBetween(date1, date2) <= 366 # Account for leap years

    return False

```

The general approach I think makes sense - first, check if they're within a year - easy. If they're a year apart, check if adding a year's worth of days to one gives you the same date as the other.

There's possibly a more elegant way to do this with the `java.time` API, but we don't have any nice helpers for that library, so it would end up pretty awkward to use.

---

<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:** [February 4, 2025, 5:27pm UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/6 "2025-02-04T17:27:24Z")

</div>

Given `t0` and `t1`:  
Use `t1x = system.date.addYears(t0, 1)` with the earlier endpoint. Check `t1.time == t1x.time`.

That will check for exactly a year apart, to the millisecond. Use integer divide by 1000 to ignore millis.

`.addYears()` handles leap years.

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [February 4, 2025, 8:52pm UTC](https://forum.inductiveautomation.com/t/system-date-getdayofmonth-unexpected-output/101808/7 "2025-02-04T20:52:36Z")

</div>

Thanks Phil, that's a very elegant and easy solution to my problem - and it seems so obvious now that you mention it. I implemented it into my project and it works great!
