Y.G.J
Y.G.J

Reputation: 1108

how to use js with my server date and time

i want my server time and date to be set as new date i have tried this:

   dateNow = new Date("<%=now()%>");

but this is not working how can i do this?

here is the code that give me the problem... it is just get stack on the time diff and doesn't countdown

<script type="text/javascript">
dateFuture = new Date(<%=year(privatesellstartdate)%>,<%=month(privatesellstartdate)-1%>,<%=day(privatesellstartdate)%>,<%=hour(privatesellstarttime)%>,<%=minute(privatesellstarttime)%>,00);

function GetCount(){
dateNow = new Date(<%=year(date())%>,<%=month(date())-1%>,<%=day(date())%>,<%=hour(time())%>,<%=minute(time())%>,00);               //grab current date
    amount = dateFuture.getTime() - dateNow.getTime();      //calc milliseconds between dates
    delete dateNow;

    // time is already past
    if(amount < 0){
        document.getElementById('countbox').innerHTML="Now!";
}
    // date is still good
    else{
        days=0;hours=0;mins=0;secs=0;out="";

        amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

        days=Math.floor(amount/86400);//days
        amount=amount%86400;

        hours=Math.floor(amount/3600);//hours
        amount=amount%3600;

        mins=Math.floor(amount/60);//minutes
        amount=amount%60;

        secs=Math.floor(amount);//seconds

        if(days != 0){out += days +":";}
        if(days != 0 || hours != 0){out += hours +":";}
        if(days != 0 || hours != 0 || mins != 0){out += mins +":";}
        out += ((secs>=10)?secs:"0"+secs) ;
        document.getElementById('countbox').innerHTML=out;

        setTimeout("GetCount()", 1000);
    }
}
window.onload=GetCount;//call when everything has loaded

</script>

Upvotes: 0

Views: 631

Answers (3)

Dave Ward
Dave Ward

Reputation: 60580

VBScript's Now returns the date and the time like:

5/27/2010 6:06:39 PM 

JavaScript's Date() constructor doesn't like that particular format. Instantiating a JavaScript Date with time from a string requires the time to be in 24h format. It looks like you're trying to cobble it together correctly in the code you pasted though. Not sure why that isn't working.

My question is why do the date comparison on the client-side at all? VBScript's DateDiff would take care of this for you on the server-side:

var seconds = <%= DateDiff("s", Now, privatesellstartdate) %>;

if (seconds > 0) {
  var days = <%= DateDiff("d", Now, privatesellstartdate) %>;

  var hours = <%= DateDiff("h", Now, privatesellstartdate) %>;

  var minutes = <%= DateDiff("n", Now, privatesellstartdate) %>;

  // Your logic here.
}

Upvotes: 1

Pointy
Pointy

Reputation: 413702

Be careful, or shall I say, mindful, of the fact that your server and the web browsers contacting it may be in different time zones. Thus when you create a date string on the server and set up some Javascript to create a client-side date with that same string, the actual date/time will mean something different to the server and the client. Maybe that's what you want, and maybe it's not; it depends on the application.

This isn't really an answer I guess; @Alistair is right about the fact that the native Javascript Date object has a pretty limited syntax. I don't mean to shove a framework at you, but you might look at the "datejs" library ( http://www.datejs.com/ ) and see if it might help if you're doing a lot of stuff with date/time values.

Upvotes: 0

Alistair
Alistair

Reputation: 1326

I don't use ASP but looking at http://www.w3schools.com/js/js_obj_date.asp JavaScript requires the date in a particular format. Check the samples there and you should be able to retrieve the date in ASP in the correct format for it.

Upvotes: 0

Related Questions