U
U
underwater2017-10-24 19:14:59
JavaScript
underwater, 2017-10-24 19:14:59

How to make the correct date format?

I’m not friends with js at all, with grief in half I made a script to call the date and time:

$(function () {
  zk.afterMount(function () {
    function timerClock() {
      var labelOne = document.getElementById('labelOne'),
        labelTwo = document.getElementById('labelTwo');

      window.isimple = window.isimple || {}
      window.isimple.dates = window.isimple.dates || {}
      window.isimple.dates.getCurrentDate = function () {
        var d = new Date();
        var curr_date = d.getDate();
        var curr_month = d.getMonth() + 1;
        var curr_year = d.getFullYear();

        return (curr_year + "." + curr_month + "." + curr_date);
      }

      labelOne.innerHTML = window.isimple.dates.getCurrentDate();

      window.isimple.dates.getCurrentTime = function () {
        var d = new Date();
        var curr_hours = d.getHours();
        var curr_minutes = d.getMinutes();
        var curr_sec = d.getSeconds();

        return (curr_hours + ":" + curr_minutes + ":" + curr_sec);
      }

      labelTwo.innerHTML = window.isimple.dates.getCurrentTime();
    }
    setInterval(timerClock, 1000);
    timerClock()
  })

});

but if the time is like 19:05, it displays 19:5
, I understand that I need to add a check, if there is one number in minutes, add a zero to it, but how to do it doesn’t boil my head anymore.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gleendo, 2017-10-24
@dyfran

let [hours, min, sec] = new Date().toLocaleTimeString("ru").split(":");

return `${ hours = hours.length < 2 ? `0${hours}` : hours }:${ min }:${ sec }`;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question