-1

When printing the time for the clocks, a similar code works and adjusts for the timezone selected, but this does not work for printing the date. Any idea why?

It just displays the UTC default time.

<script>
    function cetDT(){

var now = 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>
ebkod
  • 103
  • 1
  • 9
  • 1
    You're using `now.getUTCDate()` and `now.getUTCHours()`, which will grab the current date and time in UTC. `getDate`, `getHours` etc. will grab timezone-dependent times. – Obsidian Age Jul 25 '17 at 22:25
  • But I'm also modifying it later with "today = new Date(today.getTime() - anHour * -2);". Or at least I'm trying.... Any suggestions? – ebkod Jul 25 '17 at 22:29
  • There are a *lot* of things wrong with this code. What are you actually trying to accomplish? – Matt Johnson-Pint Jul 25 '17 at 22:32
  • Yes it's a mess, but it'll get cleared up later. It's for a project of my own :) – ebkod Jul 25 '17 at 22:35
  • Understand that the mission of StackOverflow is to provide useful question/answer pairs to a broad audience. It is *not* about dumping code and asking others to find the bug. The above code doesn't even output the variables asked about. You also don't show any research effort. In its current form, it is likely to be (and should be) downvoted. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) from the help center documentation. – Matt Johnson-Pint Jul 25 '17 at 22:38

1 Answers1

1

You're using now.getUTCDate(), now.getUTCHours() and similar, which will grab the current date and time in UTC.

To get the local equivalent, you're looking for now.getDate(), now.getHours() etc. Note the lack of 'UTC' in the names.

Note that even though you're updating the today variable with today = new Date(today.getTime() - anHour * -2), today is initialed earlier with the UTC times. Thus, getTime() will be relative to UTC.

To resolve this, all you need to do is swap out the UTC times:

function cetDT() {

  var now = 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 that there's also several bits of code that are completely redundant, such as else { seconds = seconds; }. You may wish to look into refactoring this code ;)

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • So when swapping out the UTC time, I have to specify a timezone, if I'm correct? I'm so sorry for sounding like such a beginner, but I've been working and studying on this code and I do not understand what to replace to change the timezone. – ebkod Jul 25 '17 at 22:40
  • As bad as the question is, note that it would be better to use the UTC-based functions throughout, rather than the local-time based functions. You're right that they need to be used consistently, but the local-time functions will always take on the behaviors of the local time zone, including any daylight saving time rules that are in effect. Since the OP seems to be wanting to shift by a fixed number of hours from UTC, one should use UTC as a basis. – Matt Johnson-Pint Jul 25 '17 at 22:41
  • @ebkod - you don't even show a time zone. You show a single fixed offset of `-2`. That's very different than a time zone. Please read [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Jul 25 '17 at 22:42
  • Also read: [How to initialize javascript date to a particular timezone](https://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone). – Matt Johnson-Pint Jul 25 '17 at 22:43
  • With regards to UTC offsets, I thought the OP was attempting to calculate a time differential in their **own** timezone. Yes, daylight savings and such can cause a bit of an issue, and for a website with users in multiple countries, UTC times would be better to use throughout. I completely agree with Matt though -- `-2` is an arbitrary offset for `new Date()`. Simply do a bit of research on [**JavaScript Dates**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and you'll get it down pretty quickly :) – Obsidian Age Jul 25 '17 at 22:48
  • Note that `getTime()` is always UTC, regardless of how the Date was created. – RobG Jul 25 '17 at 22:57