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

Code Examples
Code Snippet
#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)
Code Snippet
#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)
Code Snippet
#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
Code Snippet
#This example would show how to get the current date in scripting, and extract the month and year into separate variables, and
#assign them to a "Month View" calendar object, in case you changed the viewing date and needed to return.
 
# first import the java.util.Date object required for getting the current date
from java.util import Date
 
# get calendar object we will be editing (you would have to place one by this name on your window)
cal = event.source.parent.parent.getComponent('Month View')
 
 
# get current date and separate into month and year, using dateFormat. Month View object requires an integer, so this is wrapped in int() currentDate = Date()
currentMonth = int(system.db.dateFormat(currentDate, "M"))
currentYear = int(system.db.dateFormat(currentDate, "Y"))
 
 
# change our Month View calendar object to the current month and year (provided it was not on the current month/year)
cal.month = currentMonth
cal.year = currentYear