javascript asp教程 日期相关
javascript is loosely typed. database files are not. if you put text into a boolean database column or a boolean value into a date/time column, then you will get an error.
for the most part this is not a problem, except for
take a look at the snippet below. (it's not part of our lesson, but i want you to look at it anyway.) please notice
objconn.open(strconnect) sql = "insert into folovisit (sessionid,visittime) " sql+= "values ('" + session.sessionid + "',#" + datetime + "#);" objconn.execute(sql)
datetime has to be in a very particular format:
get started:
below is the script for lesson 20.
<%@language="javascript"%> <script language="vbscript" runat="server"> dim vbtime,vbdate,vbdatetime vbtime = time & " " vbdate = date & " " vbdatetime = date & " " & time </script> <% function whattimeisit() { var m=new date() var minute=m.getminutes() var second=m.getseconds() var ampm=false if (minute >=0 && minute < 10) { minute=("0" + minute) } if (second >= 0 && second < 10 ) { second=("0" + second) } var hours=m.gethours() if (hours > 12) { ampm=true hours=hours-12 } if (hours==12) { ampm=true } if (hours == 0) { hours=hours+12 ampm=false } if (ampm) { ampm=" pm" } else { ampm=" am" } var mytime=hours + ":" + minute + ":" + second + ampm return mytime; } var datetime = new date(); var month = (datetime.getmonth() + 1) + "/"; var day = datetime.getdate() + "/"; var year = datetime.getfullyear(); var justthedate = month + day + year; var justthetime = whattimeisit(); var datetime = month + day + year + " " + whattimeisit(); response.write("<html>\r") response.write("<strong>javascript time</strong><br>\r") response.write("the date in proper database format is: " + justthedate + "<br>\r") response.write("the time in proper database format is: " + justthetime + "<br>\r") response.write("the date & time together are: " + datetime + "<br>\r") response.write("<hr>\r") response.write("<strong>vbscript time</strong><br>\r") response.write("the date in proper database format is: " + vbdate + "<br>\r") response.write("the time in proper database format is: " + vbtime + "<br>\r") response.write("the date & time together are: " + vbdatetime + "<br>\r") response.write("</html>") %>
click here to run the script in a new window.
living on vbscript time:
the other way to solve the problem is to use the vbscript date and time objects. what can i say? if you can't beat 'em, steal from 'em!
time for a tangent:
if all you want to do is store
that's the end of the lesson plan. i have received messages from all over the world. thank you for your kind words. if you have questions or comments you can click onto the link for the home page and then there's a link to contact me.