Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1
    Join Date
    Jul 2004
    Location
    I'm not quite sure.......
    Posts
    771
    Tokens
    0

    Default Displaying Time and Date through JavaScript

    Displaying Time and Date through JavaScript
    A Tutorial By Matthew License AKA Sketti

    I'm gonna start off by guessing you all know how to start off JavaScript - with <script language="JavaScript"></script> for all you people who don't know.

    Date

    Well inside the <script> tags for the basic date in an alert put this:
    Code:
    <script language="JavaScript">
    var now = new Date();
    
    alert( now );
    </script>
    That will give you:


    Now of course not everyone wants that do they? So that's when it get's an awful lot more complicated.
    Code:
    <script language="JavaScript">
    var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    
    var mons = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    
    var now=new Date();
    
    var yy = now.getYear();
    
    varmm = now.getMonth(); mm=mons[mm];
    
    var dd = now.getDate();
    
    var dy = now.getDay();    day=days[dy];
    
    alert(dy+" "+dd+" "+mm+" "+yy);
    </script>
    And that will give you this:


    Time

    If you want a basic (I say basic but it's rather hard!) time information alert you'll want this:
    Code:
    <script language="JavaScript">
    var now = new Date();
    
    var hh = now.getHours();
    
    var mn = now.getMinutes();
    
    var ss = now.getSeconds();
    
    var ms = now.getMilliseconds();
    
    var hi = "Good Morning";
    
    if( hh > 11 ) hi= "Good Afternoon";
    
    if( hh > 17 ) hi= "Good Evening";
    
    var tim = hi + "\n";
    
    tim += "Hours: " +hh+ "\n";
    
    tim += "Minutes: " +mn+ "\n";
    
    tim += "Seconds: " +ss+ "." +ms;
    
    alert(tim);
    </script>
    That will give you this:


    JavaScript Clock

    The script below displays the time according to your computer in an HTML form text input.

    Code:
    <body onLoad="tick()">
    <script language="JavaScript">
    <!--
    
    function tick(){
    var now = new Date();
    var hh = now.getHours();    if( hh <= 9 ) hh = "0" + hh;
    var mn = now.getMinutes(); if( mn<= 9 ) mn = "0" + mn;
    var ss = now.getSeconds(); if( ss <= 9 ) ss = "0" + mn;
    var tt = hh + ": " +mn+ ": " + ss;
    document.f.clock.value = tt;
    window.setTimeout( "tick()", 1000 );
    }
    
    // -->
    </script>
    <form name="f">
    <input name="clock" type="text" size="10">
    </form>
    </body>
    That code will give you this (with the current time):


    I Hope This is of some use to you. Remember! JavaScript is case sensitive so be very careful. Also try not to delete any of the brackets or semi-colons or the scripts won't work.
    Last edited by Sketti; 25-03-2005 at 09:58 AM. Reason: Spelling Mistook
    Join My Forum

  2. #2
    Join Date
    Jul 2004
    Location
    Webby Forums!
    Posts
    1,879
    Tokens
    0

    Latest Awards:

    Default

    Dont put them in alerts its just irritating :S


    Chilimagik.net // Reviews, Band Biographies, News, Pics + Loads More!!
    [Thybag.co.uk - Vive la revolutione]

  3. #3
    Join Date
    Jul 2004
    Location
    I'm not quite sure.......
    Posts
    771
    Tokens
    0

    Default

    I put the alerts in so you could see what they look like. And if your on about in the script how else are you gonna tell the time without the clock script at the bottom?
    Join My Forum

  4. #4
    Join Date
    Jul 2004
    Location
    Webdesigning Forum
    Posts
    455
    Tokens
    0

    Default

    That's great ! 10/10 Should be pinned !.
    Maybe add the script so it's a popup, like the alert, but so it comes up as an altert. Unless that's what you've done :s/
    I can't do javascript

  5. #5
    Join Date
    Jul 2004
    Location
    I'm not quite sure.......
    Posts
    771
    Tokens
    0

    Default

    It took me a while to learn the basics but once you find a book that helps teach you you're off.
    Join My Forum

  6. #6
    Join Date
    Mar 2005
    Posts
    72
    Tokens
    0

    Default

    Nice work...

    I shall refrain from posting the URL, where you can get all that information because you get al up tight about it and think I am critising your work..

    Well done

  7. #7
    Join Date
    Oct 2004
    Location
    Scotland
    Posts
    2,280
    Tokens
    1,075

    Latest Awards:

    Default

    no one likes alert boxes

    http://www.stupidian.com
    (contains mild swearing)

  8. #8
    Join Date
    Jul 2004
    Location
    Webby Forums!
    Posts
    1,879
    Tokens
    0

    Latest Awards:

    Default

    Its not as much the box just the noise it makes :| and also it lags my comp but then my comp has a mind of its own!


    Chilimagik.net // Reviews, Band Biographies, News, Pics + Loads More!!
    [Thybag.co.uk - Vive la revolutione]

  9. #9
    Join Date
    Oct 2004
    Location
    Scotland
    Posts
    2,280
    Tokens
    1,075

    Latest Awards:

    Default

    i just dont like them in general
    Last edited by -JT-; 25-03-2005 at 03:14 PM.

    http://www.stupidian.com
    (contains mild swearing)

  10. #10
    Join Date
    Jul 2004
    Location
    I'm not quite sure.......
    Posts
    771
    Tokens
    0

    Default

    I believe you can change the alert with document.write
    Join My Forum

Page 1 of 2 12 LastLast

Posting Permissions

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