How to Show Current Time in JavaScript in the Format Hh:Mm:Ss

How to show current time in JavaScript in the format HH:MM:SS?

function checkTime(i) {  if (i < 10) {    i = "0" + i;  }  return i;}
function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); // add a zero in front of numbers<10 m = checkTime(m); s = checkTime(s); document.getElementById('time').innerHTML = h + ":" + m + ":" + s; t = setTimeout(function() { startTime() }, 500);}startTime();
<div id="time"></div>

show time in HH:MM only in this javascript code

There's getHours() and getMinuets() methods available.

example:
http://jsfiddle.net/0todu2y7/

jQuery(function($) {
setInterval(function() {
var d = new Date();
var t = d.getTime();
var interval = 5*60*1000;
var last = t - t % interval;
var nextt = last + interval + 5*60000;
d.setTime(nextt);
var hours = d.getHours();
var min = d.getMinutes();
$(".clock").html(hours+":"+min);
}, 1000);
});

JavaScript seconds to time string with format hh:mm:ss

String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);

if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}

You can use it now like:

alert("5678".toHHMMSS());

Working snippet:

String.prototype.toHHMMSS = function () {    var sec_num = parseInt(this, 10); // don't forget the second param    var hours   = Math.floor(sec_num / 3600);    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);    var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;} if (minutes < 10) {minutes = "0"+minutes;} if (seconds < 10) {seconds = "0"+seconds;} return hours + ':' + minutes + ':' + seconds;} console.log("5678".toHHMMSS());

How do I get the current time only in JavaScript

var d = new Date("2011-04-20T09:30:51.01");
d.getHours(); // => 9
d.getMinutes(); // => 30
d.getSeconds(); // => 51

or

var d = new Date(); // for now
d.getHours(); // => 9
d.getMinutes(); // => 30
d.getSeconds(); // => 51

Getting current date and time in JavaScript

.getMonth() returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth() in may will return 4 and not 5.

So in your code we can use currentdate.getMonth()+1 to output the correct value. In addition:

  • .getDate() returns the day of the month <- this is the one you want
  • .getDay() is a separate method of the Date object which will return an integer representing the current day of the week (0-6) 0 == Sunday etc

so your code should look like this:

var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();

JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances

You can make use of the Date prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date object thus making it especially useful if you need to re-use this functionality.

// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}

// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}

You can then simply retrieve the date and time by doing the following:

var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();

Or call the method inline so it would simply be -

var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();

How to get current time in hh:mm:ss format using moment in React JS

This is my expected answer from @TimLewis

moment().isBetween(moment().set('hour', 21), moment().set('hour', 23)) ? 'Yes' : 'No'

How to get current time in a format hh:mm AM/PM in Javascript?

Use Date methods to set and retrieve time and construct a time string, something along the lines of the snippet.

[edit] Just for fun: added a more generic approach, using 2 Date.prototype extensions.

var now = new Date();now.setHours(now.getHours()+2);var isPM = now.getHours() >= 12;var isMidday = now.getHours() == 12;var result = document.querySelector('#result');var time = [now.getHours() - (isPM && !isMidday ? 12 : 0),             now.getMinutes(),             now.getSeconds() || '00'].join(':') +           (isPM ? ' pm' : 'am');            result.innerHTML = 'the current time plus two hours = '+ time;
// a more generic approach: extend DateDate.prototype.addTime = addTime;Date.prototype.showTime = showTime;
result.innerHTML += '<h4>using Date.prototype extensions</h4>';result.innerHTML += 'the current time plus twenty minutes = '+ new Date().addTime({minutes: 20}).showTime();result.innerHTML += '<br>the current time plus one hour and twenty minutes = '+ new Date().addTime({hours: 1, minutes: 20}).showTime();result.innerHTML += '<br>the current time <i>minus</i> two hours (format military) = '+ new Date().addTime({hours: -2}).showTime(true);result.innerHTML += '<br>the current time plus ten minutes (format military) = '+ new Date().addTime({minutes: 10}).showTime(true);

function addTime(values) { for (var l in values) { var unit = l.substr(0,1).toUpperCase() + l.substr(1); this['set' + unit](this['get' + unit]() + values[l]); } return this;}
function showTime(military) { var zeroPad = function () { return this < 10 ? '0' + this : this; }; if (military) { return [ zeroPad.call(this.getHours()), zeroPad.call(this.getMinutes()), zeroPad.call(this.getSeconds()) ].join(':'); } var isPM = this.getHours() >= 12; var isMidday = this.getHours() == 12; return time = [ zeroPad.call(this.getHours() - (isPM && !isMidday ? 12 : 0)), zeroPad.call(this.getMinutes()), zeroPad.call(this.getSeconds()) ].join(':') + (isPM ? ' pm' : ' am');
}
<div id="result"></div>

Current time formatting with Javascript

A JavaScript Date has several methods allowing you to extract its parts:

getFullYear() - Returns the 4-digit year

getMonth() - Returns a zero-based integer (0-11) representing the month of the year.

getDate() - Returns the day of the month (1-31).

getDay() - Returns the day of the week (0-6). 0 is Sunday, 6 is Saturday.

getHours() - Returns the hour of the day (0-23).

getMinutes() - Returns the minute (0-59).

getSeconds() - Returns the second (0-59).

getMilliseconds() - Returns the milliseconds (0-999).

getTimezoneOffset() - Returns the number of minutes between the machine local time and UTC.

There are no built-in methods allowing you to get localized strings like "Friday", "February", or "PM". You have to code that yourself. To get the string you want, you at least need to store string representations of days and months:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

Then, put it together using the methods above:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];var d = new Date();var day = days[d.getDay()];var hr = d.getHours();var min = d.getMinutes();if (min < 10) {    min = "0" + min;}var ampm = "am";if( hr > 12 ) {    hr -= 12;    ampm = "pm";}var date = d.getDate();var month = months[d.getMonth()];var year = d.getFullYear();var x = document.getElementById("time");x.innerHTML = day + " " + hr + ":" + min + ampm + " " + date + " " + month + " " + year;
<span id="time"></span>

How to get current time HH:MM:SS in Audio element?

//// get current time 
myPlayer.addEventListener("timeupdate", function(){
myRange.value = myPlayer.currentTime;
tt = "0";
var Amin = Math.floor(myPlayer.currentTime/60);

var Asec = Math.floor(myPlayer.currentTime - Amin * 60);

if(Asec < 10){
Asec = "0" + Asec;
}
if(Amin > 10){
tt="";
}
currenttime.innerHTML = tt+Amin+":"+Asec;
});


Related Topics



Leave a reply



Submit