Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:31:03+00:00 2026-06-01T09:31:03+00:00

So I created this clock which displays Date and Time. Is there a more

  • 0

So I created this clock which displays Date and Time.
Is there a more elegant way to write this code, because for some reason it looks pretty messy, even though it does what i want it to do.

Thanks

Here is the code:
http://jsfiddle.net/vkramer/X4PMg/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:31:04+00:00Added an answer on June 1, 2026 at 9:31 am

    Yes. Let’s ignore the nearly-minifed CSS for the moment. JavaScript has array literals. You should use those. So this:

    var showClock = function(){  
       var now = new Date();
       var hours = now.getHours();  
       var minutes = now.getMinutes();  
       var seconds = now.getSeconds();
    
       if ( hours < 10 ){
        hours = "0" + hours;
       }
       if ( minutes < 10 ){
        minutes = "0" + minutes;
       }
       if ( seconds < 10 ){
        seconds = "0" + seconds;
       }
       document.getElementById("hours").innerHTML = hours;
       document.getElementById("minutes").innerHTML = minutes;
       document.getElementById("seconds").innerHTML = seconds;
    
    
       setTimeout("showClock();", 100);
    };  
    
    var showDate = function(){
        var now = new Date();  
        var d = now.getDay();
        var m = now.getMonth();
        var y = now.getFullYear();
        var dayOfMonth = now.getDate();
    
        var day_name = new Array(7);
            day_name[0]="Sunday"
            day_name[1]="Monday"
            day_name[2]="Tuesday"
            day_name[3]="Wednesday"
            day_name[4]="Thursday"
            day_name[5]="Friday"
            day_name[6]="Saturday"
    
        var month_name = new Array(11);
            month_name[0] = "January"
            month_name[1] = "February"
            month_name[2] = "March"
            month_name[3] = "April"
            month_name[4] = "May"
            month_name[5] = "June"
            month_name[6] = "July"
            month_name[7] = "August"
            month_name[8] = "September"
            month_name[9] = "October"
            month_name[10] = "November"
            month_name[11] = "December"
    
    
    
       document.getElementById("day").innerHTML = day_name[now.getDay()];
       document.getElementById("month").innerHTML = month_name[now.getMonth()];
       document.getElementById("year").innerHTML = y;
       document.getElementById("dayOf").innerHTML = dayOfMonth;
    
    }
    showClock();
    showDate();
    

    Becomes this:

    var showClock = function() {  
       var now = new Date();
       var hours = now.getHours();  
       var minutes = now.getMinutes();  
       var seconds = now.getSeconds();
    
       if ( hours < 10 ) {
           hours = "0" + hours;
       }
       if ( minutes < 10 ) {
           minutes = "0" + minutes;
       }
       if ( seconds < 10 ){
           seconds = "0" + seconds;
       }
    
       document.getElementById("hours").innerHTML = hours;
       document.getElementById("minutes").innerHTML = minutes;
       document.getElementById("seconds").innerHTML = seconds;
    
       setTimeout("showClock();", 100);
    };  
    
    var showDate = function() {
        var now = new Date();  
        var d = now.getDay();
        var m = now.getMonth();
        var y = now.getFullYear();
        var dayOfMonth = now.getDate();
    
        var day_name = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    
        var month_name = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    
    
       document.getElementById("day").innerHTML = day_name[now.getDay()];
       document.getElementById("month").innerHTML = month_name[now.getMonth()];
       document.getElementById("year").innerHTML = y;
       document.getElementById("dayOf").innerHTML = dayOfMonth;
    }
    
    showClock();
    showDate();
    

    Then, never pass a string to setTimeout. Don’t make variables and not use them.

    function showClock() {  
       var now = new Date();
       var hours = now.getHours();  
       var minutes = now.getMinutes();  
       var seconds = now.getSeconds();
    
       if(hours < 10) {
           hours = "0" + hours;
       }
    
       if(minutes < 10) {
           minutes = "0" + minutes;
       }
    
       if(seconds < 10) {
           seconds = "0" + seconds;
       }
    
       document.getElementById("hours").innerHTML = hours;
       document.getElementById("minutes").innerHTML = minutes;
       document.getElementById("seconds").innerHTML = seconds;
    
       setTimeout(showClock, 100);
    } 
    
    function showDate() {
        var now = new Date();
    
        var day_name = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        var month_name = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    
       document.getElementById("day").innerHTML = day_name[now.getDay()];
       document.getElementById("month").innerHTML = month_name[now.getMonth()];
       document.getElementById("year").innerHTML = now.getFullYear();
       document.getElementById("dayOf").innerHTML = now.getDate();
    }
    
    showClock();
    showDate();
    

    Other stuff, too, but this makes it acceptable. Also, stop pretending to use HTML5. A <section> is completely wrong there, you should just use a <div>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created this regular expression to validate names: ^[a-zA-Z0-9\s\-\,]+.\*?$ Is there a way add
I have created this php script which displays the contents of a designated directory
I have dialog created like this $('#add_error').click(function(e) { $('<div>') .load('/someaction/format/html/') .dialog({ title: 'Some title',
I have a Processing code which displays words from a text file. I have
I have created a listview which displays all the people in the database. But
i am storing member's created date using the time() function, and echo them back
I want to create a Alarm clock app. And as i know for this
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I created this layout of successive text input fields, 1- Enter data into empty
I created this regex to match all words that start with @ in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.