Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Tweeted twitter.com/StackCodeReview/status/832939044866764801
deleted 122 characters in body; edited title; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

How to make the JavaScript clock js code better so that it runs faster and is smaller in size?

I am trying to build a clock. You can see the working code here

http://codepen.io/hellouniverse/pen/LxOjNKhere

 . However, I feel like I can do much better in the javaScript code.

May you someone help me to make the js code better please? TheJavaScript code works as it is. However, I want it to be better please

var getDate = function getDate() {
    var date = new Date();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var ampm = 'pm'; // by default, it is pm
    var showDate;
    var showTime;

    if (hour < 10) {
        hour = '0' + hour;
    }

    if (minutes < 10) {
        minutes = '0' + minutes;
    }

    if (hour < 12) {
        ampm = 'am';
    }

    if (hour > 12) {
        hour -= 12;
    }

    showDate = monthNames[month] + ' ' + day;
    showTime = hour + ':' + minutes + ampm;
    document.getElementById('js-date').innerHTML = showDate;
    document.getElementById('js-time').innerHTML = showTime;
    requestAnimationFrame(getDate);
};

getDate();

How to make the clock js code better so that it runs faster and is smaller in size?

I am trying to build a clock. You can see the working code here

http://codepen.io/hellouniverse/pen/LxOjNK

  However, I feel like I can do much better in the javaScript code.

May you someone help me to make the js code better please? The code works as it is. However, I want it to be better please

var getDate = function getDate() {
    var date = new Date();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var ampm = 'pm'; // by default, it is pm
    var showDate;
    var showTime;

    if (hour < 10) {
        hour = '0' + hour;
    }

    if (minutes < 10) {
        minutes = '0' + minutes;
    }

    if (hour < 12) {
        ampm = 'am';
    }

    if (hour > 12) {
        hour -= 12;
    }

    showDate = monthNames[month] + ' ' + day;
    showTime = hour + ':' + minutes + ampm;
    document.getElementById('js-date').innerHTML = showDate;
    document.getElementById('js-time').innerHTML = showTime;
    requestAnimationFrame(getDate);
};

getDate();

JavaScript clock

I am trying to build a clock. You can see the working code here. However, I feel like I can do much better in the JavaScript code.

var getDate = function getDate() {
    var date = new Date();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var ampm = 'pm'; // by default, it is pm
    var showDate;
    var showTime;

    if (hour < 10) {
        hour = '0' + hour;
    }

    if (minutes < 10) {
        minutes = '0' + minutes;
    }

    if (hour < 12) {
        ampm = 'am';
    }

    if (hour > 12) {
        hour -= 12;
    }

    showDate = monthNames[month] + ' ' + day;
    showTime = hour + ':' + minutes + ampm;
    document.getElementById('js-date').innerHTML = showDate;
    document.getElementById('js-time').innerHTML = showTime;
    requestAnimationFrame(getDate);
};

getDate();
Source Link

How to make the clock js code better so that it runs faster and is smaller in size?

I am trying to build a clock. You can see the working code here

http://codepen.io/hellouniverse/pen/LxOjNK

However, I feel like I can do much better in the javaScript code.

May you someone help me to make the js code better please? The code works as it is. However, I want it to be better please

var getDate = function getDate() {
    var date = new Date();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var ampm = 'pm'; // by default, it is pm
    var showDate;
    var showTime;

    if (hour < 10) {
        hour = '0' + hour;
    }

    if (minutes < 10) {
        minutes = '0' + minutes;
    }

    if (hour < 12) {
        ampm = 'am';
    }

    if (hour > 12) {
        hour -= 12;
    }

    showDate = monthNames[month] + ' ' + day;
    showTime = hour + ':' + minutes + ampm;
    document.getElementById('js-date').innerHTML = showDate;
    document.getElementById('js-time').innerHTML = showTime;
    requestAnimationFrame(getDate);
};

getDate();