Click to See Complete Forum and Search --> : Is it possible to get server time with Javascript?


defrag79
05-08-2003, 02:22 PM
I have a script that currently grabs the client's time and displays in on the websites.

I was wondering if there is a way to get the local time of the web server using the same script (see below).

Any help would be greatly appreciated.

//
// Digits are of the form #fdb.gif, where # is 0 to 9 inclusive.
// The AM indicator is afdb.gif
// The PM indicator is pfdb.gif
// The colon seperator is cfdb.gif

var timerID = 0
var digitPrefix = "Images/digiclock/"
var digitSuffix = "fdb.gif"
var dateNow
var hours
var mins
var secs
var digits
var amDigit
var pmDigit
var ampm

digits = new Array(10)
amDigit = new Image()
pmDigit = new Image()

for ( i=0; i<10; i++ ) {
digits[i] = new Image()
digits[i].src = digitPrefix + i + digitSuffix
}

amDigit.src = digitPrefix + "a" + digitSuffix
pmDigit.src = digitPrefix + "p" + digitSuffix

// The free-running timer function. It is designed to update the
// digital clock once every second.

function DigitalClock() {
dateNow = new Date()
hours = dateNow.getHours()

// Adjust the time for standard am/pm display.

ampm = ( hours > 11 ? pmDigit : amDigit );

if ( hours > 12 )
hours -= 12;

if ( hours == 0 )
hours = 12;

// Now build up the digit prefixes for the gif string.

var i = hours % 10;
document.images["d0"].src = digits[((hours-i) / 10)].src;
document.images["d1"].src = digits[i].src;
document.images["d8"].src = ampm.src;

mins = dateNow.getMinutes();
i = mins % 10;
document.images["d3"].src = digits[((mins-i) / 10)].src;
document.images["d4"].src = digits[i].src;

secs = dateNow.getSeconds();
i = secs % 10;
document.images["d6"].src = digits[((secs-i) / 10)].src;
document.images["d7"].src = digits[i].src;

// Reset the timer.

timerID = setTimeout('DigitalClock()',1000);
}

function tinit() {
timerID = setTimeout('DigitalClock()',100);
isLoaded = true;
}

Jona
05-08-2003, 03:06 PM
I don't think it's possible to get the server's time without using a server-side language.

havik
05-08-2003, 03:10 PM
Since Javascript is a client-side language, I'd have to agree with Jona.

Havik

Jona
05-08-2003, 03:13 PM
L:)L

defrag79
05-08-2003, 03:24 PM
Obviously I am not a javascript expert :) I specialize in Cold Fusion.

Does anyone have any ideas as to how to get the server time using Server Side scripting than adding it to the script outlined above?

Jona
05-08-2003, 03:27 PM
Originally posted by defrag79
I specialize in Cold Fusion.

:rolleyes: *Glares at Havik...* You've got this one, buddy. :p I don't know CF.

khalidali63
05-08-2003, 03:45 PM
It should be as easy as it can be,as you said you know cold Fusion,there must be some code that you can get system time with..something like in JSP/Java

Date date = new Date();
//convert it to a string

String sDate = String.valueOf(date)

//and now print this date on to a page

<b>Current Time :<%= sDate %> </b>

The above line will get the System time from web server and print it on the page,
I am pretty sure there is code to do this in CF

havik
05-08-2003, 05:36 PM
:rolleyes: *Glares at Havik...* You've got this one, buddy. :p I don't know CF.

lol :D

try this...

<cfset newTime = dateadd('h',3,now())>
<cfoutput>
#timeformat(newTime,"HH:MM:SS")#
</cfoutput>

The 3 I entered as a dateadd parameter means that the time will be offset 3 hours early from your servers time (in case your server is in another time zone. Change it to meet your needs.

Havik

defrag79
05-09-2003, 07:54 AM
Ok, thank you for the advice...my question is this:

Will it still allow for "ticking time" (live clock where the digits count like a digital clock in live/real time)?

I know how to show the current time with Cold Fusion (static, no countdown) but not live running time.

Jona
05-09-2003, 08:49 AM
It's not possible with server-side to make a clock count-down. :eek:

AdamGundry
05-09-2003, 09:05 AM
You could use CF to get the correct (static) time, pass that to Javascript, then use JS to handle the countdown. Most JS countdown scripts could be modified to do what you need, as the JS Date object can accept a specific date as a parameter (i.e. use the date from ColdFushion).

Adam

Jona
05-09-2003, 09:08 AM
That's a different story. :p