dateFormat returning the wrong year

Hey all,

I hope your holidays have been going well!

Just looking for a quick sanity check and maybe some pointers regarding an expression that I'm using on a label component in perspective.

The specific function is:

dateFormat(NOW(), "YYYY")

I'm having a very confusing time as it is currently returning 2025. This post was created at around 11:30AM on Dec. 30th 2024 local time. I've checked PC time settings, local PC time settings, and pretty much everything else I can think of anyways.

For reference, now() by itself seems to return the correct date and time, not 2025.

Any advice would be appreciated. Thanks!

Use lower case.

(Look closer at the pattern definitions.)

1 Like

See dateFormat | Ignition User Manual

which references
https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

which gives,

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
1 Like

That old table doesn't show the difference between upper and lower case y. This one is more current:

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html

1 Like

Submitted to docs@inductiveautomation.com.

2 Likes

Thanks for the replies Phil and Transistor, lowercase did solve my problem.

Just to round this off a bit, YYYY returns the "week year", or the year that a week belongs to. (As far as I can make out) Week 1 contains the 1st January and since that's a Wednesday in 2025 the preceeding Monday and Tuesday are also in Week 1 of 2025. So (for my locale, Ireland), I get,
dateFormat(toDate("2024-12-29 08:00:00"), "YYYY") → 2024
dateFormat(toDate("2024-12-30 08:00:00"), "YYYY") → 2025
dateFormat(toDate("2024-12-31 08:00:00"), "YYYY") → 2025
dateFormat(toDate("2025-01-01 08:00:00"), "YYYY") → 2025


Figure 1. Week 1 of 2025 contains the last two days of 2024, so the Week Year is 2025 for those two dates.

There's some discussion over on StackOverflow which goes into some detail - but who has time?