javascript - Why does the date not adjust to the time set by UTC? -


when printing time clocks, similar code works , adjusts timezone selected, not work printing date. idea why?

it displays utc default time.

<script>     function cetdt(){  var = new date(); var today = new date(now.getutcfullyear(), now.getutcmonth(), now.getutcdate(),  now.getutchours(), now.getutcminutes(), now.getutcseconds());  var day = today.getdate(); var month = today.getmonth(); var year = today.getfullyear();  var anhour = 1000 * 60 * 60; today = new date(today.gettime() - anhour * -2);  var hours = today.gethours(); var minutes = today.getminutes(); var seconds = today.getseconds();  if (hours >= 12){ meridiem = ""; } else { meridiem = ""; }   if (minutes<10){ minutes = "0" + minutes; } else { minutes = minutes; }  if (seconds<10){ seconds = "0" + seconds; } else { seconds = seconds; }  document.getelementbyid("cetdt").innerhtml = (day + '/' + (parsefloat (month) + 1) + '/' + year);  }  cetdt(); </script> 

you're using now.getutcdate(), now.getutchours() , similar, grab current date , time in utc.

to local equivalent, you're looking now.getdate(), now.gethours() etc. note lack of 'utc' in names.

note though you're updating today variable today = new date(today.gettime() - anhour * -2), today initialed earlier utc times. thus, gettime() relative utc.

to resolve this, need swap out utc times:

function cetdt() {    var = new date();   var today = new date(now.getfullyear(), now.getmonth(), now.getdate(), now.gethours(), now.getminutes(), now.getseconds());    var day = today.getdate();   var month = today.getmonth();   var year = today.getfullyear();    var anhour = 1000 * 60 * 60;   today = new date(today.gettime() - anhour * -2);    var hours = today.gethours();   var minutes = today.getminutes();   var seconds = today.getseconds();    if (hours >= 12) {     meridiem = "";   } else {     meridiem = "";   }     if (minutes < 10) {     minutes = "0" + minutes;   } else {     minutes = minutes;   }    if (seconds < 10) {     seconds = "0" + seconds;   } else {     seconds = seconds;   }    document.getelementbyid("cetdt").innerhtml = (day + '/' + (parsefloat(month) + 1) + '/' + year);  }  cetdt(); 

note there's several bits of code redundant, such else { seconds = seconds; }. may wish refactoring code ;)

hope helps! :)


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -