Basic Question

New to Ignition, trying to get a handle on Scripting. First thing I tried in the interactive script tester:

import datetime
system.db.dateFormat(now(), “MMM d, h:mm a”)

says now is not defined…What am I missing?

The function now() only exists in expression binding. You have to do this:from java.util import Date print system.db.dateFormat(Date(), "MMM d, h:mm a")

A couple things.

First, to access that now() function, you would use:

import datetime
datetime.datetime.now()

Second, system.db.dateFormat() requires a java.util.Date object, not a Python datetime object. You can format the datetime object using docs.python.org/library/datetime … e.strftime

  • OR -

Go with what Travis did :slight_smile:

Hi there!

Now that I’ve used it for a while, I tend to use the java.util.calendar object if I need a now() kind of function in a script:

from java.util import Calendar x=Calendar.getInstance() print x.time print system.db.dateFormat(x.time, "MMM d, h:mm a")

Or go with what Travis did… :laughing:

That information is helpful. Thank you. The scripting examples in the Ignition help need some more detail. Thanks again