0

I have a website which relies on current dates, I'm using javascript to display events depending on what day it is. I'm trying to get this to display according to the server's time.

//monday code detection
var d = new Date();
var s = d.getDay();
var r = d.getHours();
if ((s > 1 || s == 0) && (r > 5 || s == 3 || s == 4 || s == 5 || s == 6 || s == 0)) {
  window.location = "missed/mon.html";
}
Max Njoroge
  • 489
  • 3
  • 21

1 Answers1

0

If you only want the server datetime when the page loads you could declare and initialise d like this (inserting ASP into the JavaScript):

var d = new Date("<% =formatDateTime(now(),1) & " " & formatDateTime(now(),3) %>");

or, you if your server allows, you could declare a "jscript" script to run at the server which exposes a jscript function to ASP:

<script language="jscript" type="text/jscript" runat="server">
    function getServerTime() { return new Date();  }
</script>

<script type="text/javascript">
    var d = new Date("<% =getServerTime() %>");
</script>

If you want it to happen on an event like a button click then you could consider an ajax call to get the server datetime. Muhammed (above in the comments) has given an example and here's another : http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_suggest_asp.

The server side ASP code could just return the same as the above (<% =formatDateTime(now(),1) & " " & formatDateTime(now(),3) %>)

I am assuming your users share the same timezone as the server and you won't have to do any timezone offset conversions.

I hope this helps. Best regards.

Here's my working:

<script language="jscript" type="text/jscript" runat="server">
    function getServerTime() { return new Date();  }
</script>

<script type="text/javascript">
    var d0 = new Date();
    document.write("<br/><br/>Browser: " + d0);

    var d1 = new Date("<% =formatDateTime(now(),1) & " " & formatDateTime(now(),3) %>");
    document.write("<br/><br/>Server 1: " + d1);

    var d2 = new Date("<% =getServerTime() %>");
    document.write("<br/><br/>Server 2: " + d2);
</script>

<%
response.write "<br/><br/>Server in ASP 1: " & getServerTime()
response.write "<br/><br/>Server in ASP 2: " & now()
%>