Power Table Foreground Color Based on Date

I have a power table with a hidden datetime column coming from a DB. If the date in the row is older than today’s date, I want the text color to be red. Here is the code I tried to no avail

	from datetime import datetime
	now = datetime.now()
	schDate = self.data.getValueAt(rowIndex, 'schdate')
	if now > schDate:
		return {'foreground':'red'}
		return {'foreground':'black'}

You’ll need to find out what type the values in your “schdate” column are. One thing I’m pretty sure of is that they are not of type datetime.datetime.

Once you determine their type it should be relatively easy to find out how to correctly do your comparison.

1 Like

It’s coming from a MySQL DB, the column type is datetime.

I got it to work by converting the dates into the integer format of 20190402
I changed the MySQL select to display the date the same way of YYYYmmdd as int
then compared.

	from datetime import datetime
	now = datetime.now()
	date = int(now.strftime('%Y%m%d'))
	schDate = int(self.data.getValueAt(rowIndex, 'schdate'))

Just to explicitly state what may already be obvious to you; MySQL DATETIME is not at all the same thing as Jython datetime.

That is why you had to convert both objects to the same type in order to do the comparison.

1 Like

Avoid the jython datetime module. Use java.util.Date instead, as that is the actual type (or supertype) returned by JDBC for datetime and timestamp columns. That is also the type returned by Ignition’s system.date.* functions, for the same reason. For more complex operations than java.util.Date is capable of, nor provided by Ignition, use java.util.Calendar.
Note that java’s Date and Calendar objects have .before() and .after() comparison methods for your convenience.

Seriously, don’t use jython’s datetime module.