简体   繁体   中英

How do I compare an ADO field to the current time in ASP Classic/JavaScript?

My SQL Server database table has a time(7) field. I can access this table from an ASP script using the usual ADODB.Recordset methods. I want to then compare the time retrieved to the current time. I'm guessing this'll need a JavaScript Date object, since that's the language this version of ASP is using. Problem is, I'm having trouble finding a way to compare the ADO Field object to a JS Date.

Basically, I want to write a line like

var pastTime = hasTimeComeYet(rs.Fields.Item("Time"));

but I don't know how to write the hasTimeComeYet function.

EDIT: I've cast the field to datetime (with appropriate modifications for date) as part of the SQL query. Unfortunately, I'm stuck with JScript, so I won't be able to use any functions that are only available in VBScript.

Assuming that the SQL Server query is returning an appropriate date/time data type, the Recordset will return a variant of subtype VT_DATE. In JScript, you can simply pass this value to the Date constructor and it will construct a JScript Date object.

var jsDateFromDB = new Date(rs.Fields.Item("theTime").Value);

A few notes about the example:

  • I can't remember if the .Value is required or not. If JScript doesn't expand default properties, then you need the .Value , otherwise you're just getting the Field object and not the actual date value. I would put it just to be explicit in either case.
  • I renamed the column to "theTime" only to avoid using a reserved name. I'm sure it's just an example name anyway.

I'm assuming that you know how to do the date comparison in JScript, or at least can easily look it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM