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


i have jquery function uses moment.js calculate time in different timezone based on clock in 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, when user changes system clock, momentjs shows incorrect time. figured out can fetch timestamp server (by php) , use start time updating time on client's webpage. tried write script 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 doesn't work. want have same result after running first script, difference works independently user's clock in system. how can that? here fiddle: http://jsfiddle.net/b8o5cvdz/9/

there error here:

function updatetimebasedonserver(){             var timestamp = 1443618723;             var chicago = new date(timestamp); // here create date object             var datestring = chicago.format('h:mm:ss a'); // date object not have 'format' method. moment.js object has it.            $('#time2').html( datestring +", "); }; 

to fix it, use moment.js object:

function updatetimebasedonserver() {     var timestamp = 1443618723;     var chicago = moment(timestamp); // <- here create moment.js object time stamp     var datestring = chicago.format('h:mm:ss a');     $('#time2').html(datestring + ", "); }; 

here can find jsfiddle working code.

to answer second question in comment:

thanks, 1 more thing - 2nd date not refresh every second - there way of fixing it?

you should not hard code timestamp value (1443618723) inside updatetimebasedonserver() because @ every call use value. reason not see time change.

to fix it, move timestamp outside method:

function updatetimebasedonserver(timestamp) { // take in input 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 timestamp @ every call.     updatetimebasedonserver(timestamp); }, 1000); 

se jsfiddle updated code.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -