-3

Caould anbody please help me, how I can change my javascript code to show year 20 not 2020. I am trying to add this code

d = Date.now();
    d = new Date(d);
    d = d.getDate()+'.'+(d.getMonth()+1)+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()  
      console.log(d);

But I cannot it get it write, how i should add this code to my code to get it right,. Thank you in advice

<script>
const pad = num => ("0" + num).slice(-2);
const timedate = () => {
  const currentTime = new Date(new Date().getTime() + diff);
  let hours = currentTime.getHours();
  const minutes = pad(currentTime.getMinutes());
  const seconds = pad(currentTime.getSeconds());

  const d = currentTime.getDate();


    console.log(d);
  const day = pad(d);
  const month = pad(currentTime.getMonth() + 1);
  const yyyy = currentTime.getFullYear();


/*  let dn = "PM"
  if (hours <= 12) dn = "AM";
  if (hours >= 12) hours -= 12;
  if (hours == 0) hours = 12; */
  hours = pad(hours);
  timeOutput.value = "" +
    yyyy + "." + month + "." + day +
    " " +
    hours + ":" +
    minutes + ":" +
    seconds// + dn;
}
let timeOutput;
let serverTime;
let diff;
window.addEventListener("load", function() {
  timeOutput = document.getElementById("timedate");
  serverTime = new Date;// change to new Date("[[:Date:]]"); for example
  diff = new Date().getTime() - serverTime.getTime();
  setInterval(timedate, 1000);

});
</script>
  • Go _read up_ on what methods the Date object provides, and pick a more appropriate one than `getFullYear`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – CBroe Apr 03 '20 at 11:11
  • Just cut it in half `String(1234).substring(2,4); // "34"`. Note that this will no longer work when the year becomes 5 digits. thankfully it leaves 7980 years to improve. – Gavin Apr 03 '20 at 11:13

1 Answers1

0
new Intl.DateTimeFormat("en", {year: "2-digit"}).format(new Date())
Emmanuel Demey
  • 2,158
  • 4
  • 18
  • 21