skip to Main Content

Hello i have this code js retrun erro in console

edited added full code

let now = new Date();
// we need a function that makes hours and minutes a two digit number
Object.prototype.twoDigits = function() {
  return ("0" + this).slice(-2);
}


// compile the current hour and minutes in the format 09:35
timeOfDay = now.getHours().twoDigits() + ':' + now.getMinutes().twoDigits();

// test if timeOfDay is within a given time frame
if ('05:01' <= timeOfDay && timeOfDay <= '23:59') {
  document.getElementById('skryj').style.display = 'none';
}

Error is:

Uncaught TypeError: V[a].exec is not a function

How to fix dont much understand JS thanks.

2

Answers


  1. Chosen as BEST ANSWER

    I resolve problem like this

    let now = new Date();
    
    // Utility function to format digits with leading zeros
    function twoDigits(value) {
      return value.toString().padStart(2, '0');
    }
    
    // Compile the current hour and minutes in the 09:35
    let hours = twoDigits(now.getHours());
    let minutes = twoDigits(now.getMinutes());
     timeOfDay = hours + ':' + minutes;
    
    // Test if timeOfDay is within a given time frame
    if ('05:01' <= timeOfDay && timeOfDay <= '23:59') {
      document.getElementById('skryj').style.display = 'none';
    }
    

  2. Since it’s a date.
    Other way is to use toLocaleString.

    Code;

    function twoDigits(value) {
     return value.toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search