http://www.chrispearson.org/pages/programming/vbscript/VB_Java/VB_Java_Mix.asp
17h11
Thursday, 28. August 2008

MIXED VB AND JAVA

   

The .NET Framework provides solutions to a whole range of VBScipt problems but there are still some issues with straight .asp pages that can't get these benefits.

But VBScript can use JavaScript variables and - in many instances - JavaScript can score in areas where VB doesn't. As an example, take the scenario in which you run a web site for UK users but which is hosted on a server located in the USA; on a page you want to display the current time but using VB's Now() returns 11 h 11 - the server's current time - when you know it's 16 h 11 in the UK.


JavaScript provides an excellent way round this problem in its getTimezoneOffset() method. But VBScript doesn't provide this functionality - VB can't retrieve the universal time (UTC) or the offset from the current system configuration required to calculate it.

You can, however, assign a variable in a server-side JavaScript then use that variable in later VBScripted code, as shown:
 

Start the page off in the usual way
 

     

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<HTML LANG="en">
<HEAD>
. . .
</HEAD>
<BODY>
. . .

Then add a script to get the offset for the current server time - This is the number of hours which must be added to the current server time to return UTC (GMT, that is)      

<SCRIPT language=jscript RUNAT=Server>
// Get the offset to add to convert server time to GMT
var ldNow = new Date();
var lnOffset = ldNow.getTimezoneOffset();
// lnOffset will be used by VBScript
</SCRIPT>

lnOffset can now be used to display the current UTC time in a VBScript:      
<% =Hour(Now()+(lnOffset/24)) %>h<% =Minute(Now()+(lnOffset/24)) %>
Which - in the current instance - displays 16 h 11 when lnOffset is -5 and Now() contains 28/08/2008 12:11:16 . Notice that because the offset is in hours and Now() works with days, the calculation involves adding the offfset as a fraction of a day - lnOffset/24 - to Now().      
Complete the page's HTML coding and save it with a .asp extension.
Obviously, if you're running a server in the GMT time zone the offset will be zero. But by making these calculations you also make the web site adaptable to its physical location.

NB: When I first wrote this piece my site actually was hosted in the US, now it isn't! So, to ensure that the examples work, the time zone offset data is created in the ASP (locating this page in virtual America!) before the page is served.
 

xxx,xxx

copyright ©2000 - 2008 Chris Pearson