Posts tagged with javascript

Display Server Time Using Javascript and ASP.NET in C#

November 20th, 2008

I’ve recently written an ASP.NET page using C# that required displaying the server time on the webpage using Javascript. To do this you need to output the Javascript code using C# and initialise the Javascript Date() object with the server time, after playing around I found this code does the job.

"server_time = new Date('" + DateTime.Now.ToString("MMMM d, yyyy HH:mm:ss") + "');"

The entire code looks like this assuming you have a label with id=’clock’ on the page. Just output the string js to the page to add the clock.

const string crlf = "\r\n";
string js = "<script type='text/javascript'>" + crlf +
"window.onload=startclock;" + crlf +
"var clock;" + crlf +
"var time_diff;" + crlf +
"function startclock(){" + crlf +
   "clock=document.getElementById('clock');" + crlf +
   "server_time = new Date('" + DateTime.Now.ToString("MMMM d, yyyy HH:mm:ss") + "');" + crlf +
   "time_diff=new Date()-server_time;" + crlf +
   "setInterval('runclock()',1000);" + crlf +
"}" + crlf +
"function runclock(){" + crlf +
   "var cDate=new Date();" + crlf +
   "cDate.setTime(cDate.getTime()-time_diff);" + crlf +
   "var curr_hours = cDate.getHours();" + crlf +
   "var curr_mins = cDate.getMinutes();" + crlf +
   "var curr_secs = cDate.getSeconds();" + crlf +
   "curr_hours=(curr_hours < 10)?'0' + curr_hours:curr_hours;" + crlf +
   "curr_mins=(curr_mins < 10)?'0' + curr_mins:curr_mins;" + crlf +
   "curr_secs=(curr_secs < 10)?'0' + curr_secs:curr_secs;" + crlf +
   "clock.innerHTML=curr_hours+':'+curr_mins+':'+curr_secs;" + crlf +
   "}" + crlf +
"</script>";

I should mention that if you’re using classic ASP and VBScript then the line becomes.

"server_time = new Date(" & DatePart("yyyy",Date) & "," & DatePart("m",Date)-1 & "," & DatePart("d",Date) & "," & DatePart("h",Now) & "," & DatePart("n",Now) & "," & DatePart("s",Now) & ");"
Google BookmarksEvernoteGoogle GmailHotmailWordPressLinkedInFacebookDeliciousShare

Using ADO With JavaScript

April 6th, 2008

When writing HTAs to connect to an MS Access database I used to always use VBScript.
The obvious reason for this being that it seemed easier since both are Microsoft technologies plus I initially had no idea how to create or use ActiveXObjects from JavaScript.

After doing this for a while I became very frustrated at the limitations of using VBScript so looked deeper into changing to JavaScript and found that’s it’s actually very easy to use.

As a starter for anyone else in the same position here some framework code showing how to create an ADO connection, open a recordset and execute the command object all using JavaScript.
(I know technically this is JScript, which is Microsofts version of JavaScript)

// path to database
var DBpath="\\\\Server\\Path\\myDB.mdb"

// set up a few object constants
var adLockReadOnly=1
var adOpenForwardOnly=0
var adCmdText=1

// create and open a new connection (MSAccess)
var cnn=new ActiveXObject("ADODB.connection")
cnn.Provider = "Microsoft.Jet.OLEDB.4.0;Data Source=" + DBpath
try
    {
    cnn.open
    }
catch(err)
    {
    // could not open connection
    // view details in err.Description and err.Number
    return 0
    }

//open a read only recordset
var rs = new ActiveXObject("ADODB.Recordset")
try
	{
	rs.Open("Select * from myTable", cnn, adOpenForwardOnly, adLockReadOnly)
	}
catch(err)
	{
	// could not open recordset
	return 0
	}
while(!rs.EOF)
	{
	// do something
	rs.movenext
	}
rs.close

//insert records with command object
var cmd=new ActiveXObject("ADODB.command")
cmd.ActiveConnection = cnn
cmd.CommandText = "Insert into myTables values(x ,y ,z)"
cmd.CommandType=adCmdText
    try
    {
    cmd.Execute()
    }
    catch(err)
    {
    // could not execute SQL
    return 0
    }
Google BookmarksEvernoteGoogle GmailHotmailWordPressLinkedInFacebookDeliciousShare

Switch to our mobile site