views:

85

answers:

2

When I retrived a Date field in TOAD it displayed like is '1/18/2038 9:14:07 PM',

But when I rertrived in Coldfusion using cfquery and displayed using , then I got the date on screen like '2038-01-18 21:14:07.0'.

Does anyone have idea why it displayed in different format? Is there anyway we can make it display like TOAD format?

I am using Oracle 10g DB and coldfusion 8

+5  A: 

You could use something like:

<cfquery datasource="northwind" name="queryDB">
  SELECT date_used, time_used
  FROM invoicesTable
</cfquery>

<cfoutput query="queryDB">
#DateFormat(date_used, "m/d/yyyy")#
#TimeFormat(time_used, "h:mm tt")#
</cfoutput>

I think this is what you want.

You could use

#DateTimeFormat(Now(), "mmm d, yyyy","h:mm TT")#

to have datetime format

Happy coding

Ravia
I have Date and time combined in DB like "1/18/2038 9:14:07 PM". In above example, it retrieving seperatly
CFUser
@CFUser, that's how it works in CF: dates and times formatting functions accept date object but return only own part. You can always wrap both functions into the single UDF, like FormatDateTime().
Sergii
I am not aware of a built-in function named `DateTimeFormat()`. Ben Nadel has an implementation on his blog, though. http://www.bennadel.com/blog/717-ColdFusion-DateTimeFormat-Utility-Function.htm
Tomalak
@Tomalak Yeah, I was talking about exactly such type of solution.
Sergii
+3  A: 

The Coldfusion datetime variable you retrieved from your database has all the information you are requesting. Coldfusion will automatically convert it to a variety of outputs depending on your need using some built in functions.

<!--- Assuming that 'myDateTime' is a datetime variable retrieved from cfquery --->
<cfoutput>
    <!--- Outputs: 1/7/2010 --->
    #dateFormat(myDateTime, "m/d/yyyy")#
    <!--- or use a mask shortcut - only outputs two digit year: 1/7/10 --->
    #DateFormat(myDateTime, "short")#

    <!--- Outputs: 8:47:14 AM --->
    #timeFormat(myDateTime, "h:mm:ss tt")#
    <!--- or use the shortcut: --->
    #TimeFormat(myDateTime, "medium")#
</cfoutput>

If you must construct a single string with the TOAD format, then you may concatenate the output of the dateFormat() and timeFormat() strings.

<!--- Outputs: '1/7/2010 8:47:14 AM' --->
<cfset toadFormat = dateFormat(myDateTime, "m/d/yyyy") & " " & TimeFormat(myDateTime, "medium")>

This saves a bit of hassle if you'll be using this data many times. However for most needs this is unnecessary as the original myDateTime variable contains all the information needed for all purposes other than display output.

More about DateFormat() and TimeFormat() from Adobe. More about DateFormat and TimeFormat shortcuts from Pete Freitag.

Dan Sorensen
I wasn't aware of those mask shortcuts. Thanks for that.
Al Everett