Julian Date

Is there a way to get the julian date through an expression, i.e. “dateExtract(now(),‘day’)”? I can get it through scripting, but in some cases it would be nice to have it as a bound dynamic property.

You can use dateDiff to find the number of days since the beginning of the year:

dateDiff(toDate("2012-1-1 0:00:00"), now(), "day")

Yes, but what happens next January? :slight_smile:

I assume I’d have to create another expression that gets the current year, and pass the year indirectly into this expression?

try this out

[code]import datetime

now = datetime.datetime.now()

print “Current year: %d” % now.year[/code]

You can get the current year by calling dateExtract, and then using concat to attach that year to a string that has the other parts of the date that are static ("-1-1 0:00:00"). The final expression would look like this:

dateDiff(toDate(concat(toStr(dateExtract(now(),"year")),"-1-1 0:00:00")), now(), "day")
1 Like

But that’s through scripting, and I’m already getting the date parts in other parts of the project with java.text.SimpleDateFormat. I saw in the expression docs that other date parts are available, so I
thought maybe the Julian date was just undocumented.

edit: Sorry, was responding to the post above.

I posted an expression solution after Greg’s response.

Yes, that should do it. Thanks James.

Just a question for my own curiousity- how efficient are expressions when it comes to execution times? Are they trivial compared to something like sql queries?

Expressions are more simple, the only thing that you might run into is that expressions will use more client processing power.

Just wanted to add that datediff returns a float, so if you want the Julian day to be an integer then you would need to truncate the result to avoid rounding errors:

floor(dateDiff(toDate(concat(toStr(dateExtract(now(),"year")),"-1-1 0:00:00")), now(), "day"))

That was what I was getting to. How are expressions executed? Are they constantly updated on a seperate thread? What if, instead of an expression, I got the julian date through scripting like this (maybe on a timer):

from java.util import Calendar
import java.text.SimpleDateFormat as sdf
print sdf("DDD").format(Calendar.getInstance().getTimeInMillis())

Just curious as to which approach is more efficient assuming that I'm equally comfortable programming either.

As I understand, expressions are typically more efficient than running on a timer because they fire when the window opens (InternalFrameActivate) and when their dependencies change, which typically isn’t often.

I’m not sure how frequently this expression would fire given the now() reference. Colby, Carl, Travis, Kevin - care to shed some light on this?

If (judging from the expressions we've been sharing in this thread) we're looking for the current Julian date, could this be bound to an SQLTag? I don't know if that would help the client by just referencing the tag vs. calculating it itself. On the other side, it would be calculated in one spot vs. everyone trying to do a redundant task.

Yes, the expressions can be bound to an expression tag, and then it can be reference from anywhere in any project within that gateway. As a general rule, expressions and property binding are better to use (if applicable) than scripting. It depends on the individual use case however. In this case, it depends on how many components are referencing that date, how precise/accurate the date needs to be, etc. However, if I understand what Step7 is using it for, an expression would be the most elegant and efficient way.

solved this in Ignition 8 using the following code where %ty is the 2-digit year, and %tj is the 3-digit day of the year.

concat(
stringFormat("%ty", now()),
stringFormat("%tj", now())
)

1 Like

Does it handle leap years?

and also

does (dateFormat(now(),‘D’)) also handle leap years?

(dateFormat(now(),"D"))

quotes, not apostrophes

1 Like

Yes to both. :slight_smile:

Screenshot from 2021-12-15 15-12-12

Screenshot from 2021-12-15 15-11-50

3 Likes