Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Time in 12 Hour Format in JavaScript

I want to display Time in 12 hour format by altering the following code. i Tried Various Techniques but no luck, hope to find The solution from u guys .

<script type="text/javascript">

$.fn.androClock = function() {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
function getTime() {
  var date = new Date(),
  hour = date.getHours();
  return {
    day: days[date.getDay()],
    date: date.getDate(),
    month: months[date.getMonth()],
    hour: appendZero(hour),
    minute: appendZero(date.getMinutes())
  };
}
function appendZero(num) {
  if (num < 10) {
    return "0" + num;
  }
  return num;
}
function refreshClock() {
  var now = getTime();
  $('#date').html(now.day + "<br>" + now.date + '. ' + now.month);
  $('#time').html(now.hour + ":" + now.minute);
  setTimeout(function() {
    refreshClock();
  }, 10000);
}
refreshClock();
  };
$('#andro-clock').androClock();

</script>
like image 331
Apple Arbaz Avatar asked May 13 '13 18:05

Apple Arbaz


People also ask

How do I convert 24 hours to 12 hours in react JS?

Convert the 24 hours format time to 12 hours formatted time. Now in-order to convert it to 12 hours format you can take % 12 on the current time. time = 24, then 24%12 → 0 , if the time is 0, then change the time as 12.

How do you display JavaScript datetime in 24 hour format?

Use the toLocaleString() method to change time formatting to 24 hours, e.g. date. toLocaleString('en-US', {hour12: false}) . The toLocaleString method returns a string that represents the date and time according to the provided locale and options parameters.

What is the time format in JavaScript?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.


2 Answers

EDIT

Based on your comments in rahul's answer...

Update the line:

hour: appendZero(hour),

to

hour: appendZero(((hour + 11) % 12) + 1) Live Demo


Live Demo

var formatTime = (function () {
    function addZero(num) {
        return (num >= 0 && num < 10) ? "0" + num : num + "";
    }

    return function (dt) {
        var formatted = '';

        if (dt) {
            var hours24 = dt.getHours();
            var hours = ((hours24 + 11) % 12) + 1;
            formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "pm" : "am"].join(" ");            
        }
        return formatted;
    }
})();

alert(formatTime(new Date())); 
like image 190
Brandon Boone Avatar answered Sep 24 '22 16:09

Brandon Boone


DEMO

function getTime() {
  var date = new Date(),
  hour = date.getHours();
 // var dd = "AM";
  var h = hour;
    if (h > 12) {
        h = hour-12;
   //     dd = "PM";
    }
    if (h == 0) {
        h = 12;
    }
  return {
    day: days[date.getDay()],
    date: date.getDate(),
    month: months[date.getMonth()],
    hour: appendZero(h),
    minute: appendZero(date.getMinutes()),
    //  dd: dd  
  };
}


function refreshClock() {
  var now = getTime();
  $('#date').html(now.day + "<br>" + now.date + '. ' + now.month);
 // $('#time').html(now.hour + ":" + now.minute+" "+now.dd);
  $('#time').html(now.hour + ":" + now.minute);
  setTimeout(function() {
    refreshClock();
  }, 10000);
}
like image 42
rahul maindargi Avatar answered Sep 22 '22 16:09

rahul maindargi