SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying Current Time in HTML

    Does anyone know a good way to display the current time?

    For what I want, 2 things would be acceptable - which depends on speed and size of code...

    1) Display the time that a page is accessed. This should always be displayed in GMT no matter what timezone the user is accessing from;

    2) Display the time and update it each minute or second, again in GMT.

    I've seen code that would do (1) on HtmlGoodies, but.. it doesn't seem to work.. you drop the box down to select the timezone you want to display, and it displays the time - but out by 3 hours...

    Any help would be gratefully appreciated

    Bob

  2. #2
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bob...

    What languages do you have at your disposal? Javascript is a given, but if you had PHP or ASP it would be even simpler.

    Here are some links to time scripts:

    http://www.ragecreations.com/beta/rcdatetime.html

    and here is the full list at www.hotscripts.com:

    http://www.hotscripts.com/search/?qu...ory=JavaScript
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I have a couple of scripts. One javascript which does what you need in (2). I have a page last modified script in PHP. Do you want it?

    Code:
    var itsep = 0;
    
    function show2() {
    	var Today = new Date();
    	var hours = Today.getHours();
    	var minutes = Today.getMinutes();
    	var seconds = Today.getSeconds();
    	var dn = "AM";
    	var tsep = ":";
    	
    	var WeekDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    	var WeekDay = WeekDays[Today.getDay()];
    	
    	var Months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    	var Month = Months[Today.getMonth()];
    	
    	if (hours >= 12){
    		dn = "PM";
    		hours -= 12
    	}
    	if (hours == 0)
    		hours = 12;
    	if (minutes <= 9)
    		minutes = "0" + minutes;
    	if (seconds <= 9)
    		seconds = "0" + seconds;
    		
    	tsep = itsep==0 ? ":" : ":";
    	if (++itsep==2) {itsep=0};
    	
    	
    	var ctime = "" + 
    		WeekDay + ", " + Month + " " + Today.getDate() + ", " + Today.getFullYear() + " " +
    		hours + tsep + minutes + tsep + seconds + " " + dn + "";
    	if (!document.all)
    		document.write(ctime);
    	else
    		tick.innerHTML = ctime;
    }
    
    if (document.all)
    	setInterval("show2()",500);
    if (!document.all)
    	show2();
    Place the above code in date.js and call if from your page. Where the javascript call code is, the date will appear in dynamic motion. for all other browsers except IE, the date will display, but will not 'tick'.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    Thanks for the help.

    Lyon, I'm not too hot on .js yet, I couldn't get your clock to work inside my html, no idea what I did wrong...?

    I did find a script on www.hotscripts.com which seems to work, except... it doesn't display in GMT, it displays in whatever timezone the PC is set to....

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    accept my apology.
    i forgot to include :

    <span id="date"><script type="text/javascript" src="date.js"></script></span>

    Notice the span..
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  6. #6
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    aaaarrrgghh

    Still can't get this to work :-(

    I got another script working, in GMT as well, but it's not adjusting for Daylight Savings... which should be easy enough for me to add...

    I'd still like to see your script working though, as it's -much- smaller in size than the one i found..

  7. #7
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ARGH! finally found the problem.

    change date to tick.

    <span id="tick"><script language="javascript" type="text/javascript" src="date.js"></script></span>
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  8. #8
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cool

    Thanks, got that working now :-).

    It's not GMT, but the .js code is 1/6th the size of the "working" GMT timer, so I'll stick with this one I think :-).

  9. #9
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    geez..alright. =)
    the date/time it displays is based on the user's system clock.

    sorry about all the muddled up code which led to several failed attempts of getting the script working.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  10. #10
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...

    just ignore LynLimz...he's been drinking.

    Lyon, don't drink and code man.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  11. #11
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by creole
    yeah...

    just ignore LynLimz...he's been drinking.

    Lyon, don't drink and code man.
    lol creole...the man is speaking for himself! ha!

    I'm too young to drink...at least under the eyes of the law. i would consider that a good thing eh?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •