Format Javascript Date into Hours Minute Seconds

I have this JSFiddle - http://jsfiddle.net/cH84e/1/ and I cannot get the second time to format properly. Here is the Javascript:

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);

local-time is perfect and displays hh:mm:ss

remote-time just displays a list of random numbers.

How can I make Remote Time hh:mm:ss too?

Thanks

Answers


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

And then you're displaying the result of setHours() (ms since January 1, 1970 of the new date) rather than the string.

This is what I think you're aiming for:

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

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

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

Updated JSFiddle: http://jsfiddle.net/cH84e/2/


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

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

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

Need Your Help

I can't get the return key in my UITextField's to work

ipad ios4 uitextfield return xcode4.2

I can't get the return key in my UITextField's to work!

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.