JavaScript Format JavaScript Date as Hours:Minutes:Seconds format javascript date object,format javascript date string,format javascript

I have this code and I cannot get the second time to format properly:

setInterval(function() {
  var local = new Date();
  var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" + local.getSeconds();

  var remote = new Date();
  var remotedatetime = remote.getHours() + ":" + remote.getMinutes() + ":" + remote.getSeconds();

  var remoteoffset = remote.setHours(local.getHours() - 5);

  $('#local-time').html(localdatetime);
  $('#remote-time').html(remoteoffset);
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>
Answer:1

You're adjusting remote after getting its string representation, so that's doing you no good.

Then you're displaying the result of setHours() (milliseconds since January 1, 1970) rather than the string.

This is what I think you're aiming for:

setInterval(function() {
  var local = new Date();
  var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + pad(local.getSeconds());

  var remote = new Date();
  remote.setHours(local.getHours() - 5);
  var remotedatetime = remote.getHours() + ":" + pad(remote.getMinutes()) + ":" + pad(remote.getSeconds());

  $('#local-time').html(localdatetime);
  $('#remote-time').html(remotedatetime);
}, 1000);

function pad(t) {
  var st = "" + t;
  
  while (st.length < 2)
    st = "0" + st;
    
  return st;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>
Answer:2

(Note: I asked this question on https://github.com/gkz/LiveScript/issues/731 as well) When I use LiveScript in an html file directly, I have no way to run livescript code immediately on seen. For ...

(Note: I asked this question on https://github.com/gkz/LiveScript/issues/731 as well) When I use LiveScript in an html file directly, I have no way to run livescript code immediately on seen. For ...

Are there replacements for the domReady event and the onMutate function in Polymer 0.9? In Firefox the attach event seems to work, but in Chrome the children are not in a proper state at the time of ...

Are there replacements for the domReady event and the onMutate function in Polymer 0.9? In Firefox the attach event seems to work, but in Chrome the children are not in a proper state at the time of ...

I have some route: when('/tvtest/:userid', {templateUrl: 'template/usertv', controller: SomeTest}). which loads some html with emebedded kendo-ui controls: <html> <head> <title&...

I have some route: when('/tvtest/:userid', {templateUrl: 'template/usertv', controller: SomeTest}). which loads some html with emebedded kendo-ui controls: <html> <head> <title&...

  1. kendo treeview angularjs checkbox
  2. kendo treeview angularjs example

I am new to web development and working on web project. In web project I want to show the Loader image on the postback of the page irrespective of the which control caused the postback. Currently I ...

I am new to web development and working on web project. In web project I want to show the Loader image on the postback of the page irrespective of the which control caused the postback. Currently I ...