How can I update the time from chosen timezone every minute on my webpage?

randomuser1

I have a jquery function that uses moment.js to calculate time in different timezone based on the clock in the client's system:

function updateTime(){
                var chicago = moment.tz("America/Chicago").format('h:mm:ss A');
               $('#time').html( chicago +", ");
            };

            moment.tz.add('America/Chicago|CST CDT|60 50|01010101010101010101010|1BQU0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0');                                                                         

            updateTime();
            setInterval(function(){
               updateTime();
            },1000);

It works pretty nice, however when user changes his system clock, then the momentjs shows incorrect time. I figured out I can fetch timestamp from the server (by php) and use it as a start time for updating time on client's webpage. I tried to write a script as follows:

function updateTimeBasedOnServer(){
                var timestamp = 1443618723;
                var Chicago = new Date(timestamp);
                var dateString = Chicago.format('h:mm:ss A');
               $('#time2').html( dateString +", ");
            };


            updateTimeBasedOnServer();
            setInterval(function(){
               updateTimeBasedOnServer();
            },1000);

But it doesn't work. I want to have the same result as after running the first script, but with only the difference that it works independently from the user's clock in his system. How can I do that? Here is my fiddle: http://jsfiddle.net/b8o5cvdz/9/

Andrea

There is an error here:

function updateTimeBasedOnServer(){
            var timestamp = 1443618723;
            var Chicago = new Date(timestamp); // Here you create a Date object
            var dateString = Chicago.format('h:mm:ss A'); // a Date object does not have a 'format' method. A moment.js object has it.
           $('#time2').html( dateString +", ");
};

To fix it, use a moment.js object:

function updateTimeBasedOnServer() {
    var timestamp = 1443618723;
    var Chicago = moment(timestamp); // <- Here I create a moment.js object from the time stamp
    var dateString = Chicago.format('h:mm:ss A');
    $('#time2').html(dateString + ", ");
};

Here you can find a JSFiddle with the working code.

To answer you second question in the comment:

Thanks, one more thing - the 2nd date does not refresh every second - is there a way of fixing it?

You should not hard code the timestamp value (1443618723) inside the updateTimeBasedOnServer() because at every call it will use that value. For this reason you do not see the time change.

To fix it, move the timestamp outside the method:

function updateTimeBasedOnServer(timestamp) { // Take in input the timestamp
    var Chicago = moment(timestamp);
    var dateString = Chicago.format('h:mm:ss A');
    $('#time2').html(dateString + ", ");
};

var timestamp = 1443618723;
updateTimeBasedOnServer(timestamp);
setInterval(function () {
    timestamp += 1000; // Increment the timestamp at every call.
    updateTimeBasedOnServer(timestamp);
}, 1000);

Se the JSFiddle with the updated code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

How can I extract minute from time in BigQuery?

How can I avoid "±" symbol disappearing from my script every time I restart an R session?

How can I automatically update stock prices every minute without refreshing the page?

How can I make an ASP.NET WebAPI program running in an Azure instance update a database every minute?

Moment.js: how to update the time every minute?

How can I remove a some javascript from my webpage?

How can I have `date` output the time from a different timezone?

How can I change my script so it minimizes the cost, and at the same time calculates the volume and weight chosen is viable?

Android how i can do GET request to my server every minute?

How can I make a variable "refresh" itself every time my JavaScript file has code pulled from it?

How can I update the registry values every time Windows starts?

How can I let update the time every second?

How would I get this code to update every minute?

How can I redirect users from a webpage or another webpage to a webpage

How do i upload text file to my ftp every minute?

How can I compare chosen date from calendar with dates in my database?

How can I prevent my custom directive from getting compiled/executed every time I use $route.reload()

How can I bring Electron app to foreground every "N" minute?

How can I detect when an NSTimer changes every minute?

React: how can I execute function at exact minute every hour?

How can I redo something every minute in python

How i can execute a code every one minute in php?

How can I display my quiz results by majority of answers chosen?

How can I prevent my code from yielding every frame?

How can I update my component in real time when clicking?

How can I update my GUI score one point at a time?

How can I calculate the acutal elapsed time in my timezone between two dates in C#?

How can I generate dynamic select fields on my webpage with column names and unique values from my dataframe?