Top  | Previous | Next

system.db.dateFormat

Description

This function is used to format Dates nicely as strings. It uses a format string to guide its formatting behavior. Learn more about date formatting in Working with Datatypes / Dates

 

Expert Tip: This function uses the Java class java.text.SimpleDateFormat internally, and will accept any valid format string for that class.

Syntax

system.db.dateFormat(date, formatPattern)

Parameters

Date date - The Date object that you'd like to format

String formatPattern - A format pattern string to apply.

Returns

String - The date as a string formatted according to the format pattern.

Scope

All

Examples

This example will display a message box on a button press that displays the selected date (without the time) from a Calendar component, in a format like "Feb 3, 2009"

date = event.source.parent.getComponent("Calendar").latchedDate

toDisplay = system.db.dateFormat(date, "MMM d, yyyy")

system.gui.messageBox("The date you selected is: %s" % toDisplay)

 

This example would do the same as the one above, but also display the time, in a format like: "Feb 3, 2009 8:01pm"

date = event.source.parent.getComponent("Calendar").latchedDate

toDisplay = system.db.dateFormat(date, "MMM d, yyyy")

system.gui.messageBox("The date you selected is: %s" % toDisplay)

 

This example would take two dates from two Popup Calendar components, format them in a manner that the database understands, and then use them in a SQL query to limit the results to a certain date range.

startDate = event.source.parent.getComponent("StartDate").date

endDate = event.source.parent.getComponent("EndDate").date

startDate = system.db.dateFormat(startDate, "yyyy-MM-dd HH:mm:ss")

endDate = system.db.dateFormat(endDate, "yyyy-MM-dd HH:mm:ss")

query = ("SELECT * FROM mytable WHERE t_stamp >= '%s' AND t_stamp <= '%s'" % 

 (startDate, endDate))

results = system.db.runQuery(query)

event.source.parent.getComponent("Table").data = results