Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JScript Short Date format from Access database field

Status
Not open for further replies.

rossmcm

Programmer
Jun 28, 2000
16
0
0
I have an access 2000 database that generates asp pages. One of the table fields is a date. If I display:

Code:
write (RecordSet ("Date")) ;

I get the date in the format: Tue Feb 24 21:39:29 CST 2004

but I want to display the date in short form 24-2-2004 21:39 but If I pass the recordset field to a function like:

Code:
write (FormattedShortDate (RecordSet ("Date"))) ; 

function FormattedShortDate (Date)

{
var Hours = Date.getHours () ;
if (Hours < 10)
    {
    Hours = " " + Hours ;
    }

var Minutes = Date.getMinutes () ;
if (Minutes < 10)
    {
    Minutes = "0" + Minutes ;
    }

return (Date.getDate () + "-" + Date.getMonth () + "-" + Date.getYear () + " " +
                  Hours + ":" + Minutes) ;
}

I get a runtime error (object expected) on the first line when the function executes.

How can I do this? VB has various functions for formatting dates, but the problem is, it's VB.


 
Does
Code:
FormatDateTime(RecordSet("Date"), vbShortDate) & " " & FormatDateTime(RecordSet("Date"), vbShortTime)
not work for you?

I don't know how smart it is, so it might not be able to handle your original date format, but obviously if it can then it will save you a lot of hassle.
 
Oops, sorry, I apparently ignored the "JScript" in your subject line and went with VBScript.
 
Yep, JS please. The main problem seems to be that the object returned by RecordSet ("Date") is not in fact a date, at least it isn't by the time it gets into the function. The function works fine with other date variables. I tries using Date.parse but that produces a big number (ms since 1970 I think) which also causes the function to cough.


 
Problem solved - the following works:

write (FormattedShortDate (new Date(RecordSet ("Date"))) ;

Thanks for your time.
 
in order for jscript to recognize the data being passed to it as a date you must declare it as a date.

Code:
myDate = new Date();

AND Date is a very, VERY restricted word in most langauges.
try myDate (I'm not referring to the RS but you're predefined var's)

Code:
	response.write(FormattedShortDate(RecordSet ("Date")));

	function FormattedShortDate(myDate) {
	myDate = new Date();
	var Hours = myDate.getHours() ;
	if (Hours < 10)  {
		Hours = " " + Hours ;
		}

	var Minutes = myDate.getMinutes() ;
	if (Minutes < 10) {
		Minutes = "0" + Minutes ;
		}

	return (myDate.getDate() + "-" + myDate.getMonth() + "-" + myDate.getYear() + " " + Hours + ":" + Minutes) ;
	}

___________________________________________________________________
[sub]
The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
[/sub]
 
don't forget to place the rs in the date() parm. failed to do that above

eg: var foo = new Date(rs("date"))

and along with Genimuse 's link on formatting it you should be good

___________________________________________________________________
[sub]
The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top