skip to Main Content

I would like to hide the class ".test" if it is past 2:00pm PST on 8/11/2023

 const date = new Date();
  var timeout = Fri Jun 17 2022 11:27:28 PST+0100;
  if (date >= timeout) {
     $('.text').hide();
  }

2

Answers


  1. timeout needs to be a Date object.

    var timeout = new Date('2022-06-17 11:27:28-0700')
    
    Login or Signup to reply.
  2. Your question does not match your code i.e. "if it is past 2:00pm PST on 8/11/2023" since today is 8/1/2023 not 8/11.

    But, let’s assume you get the proper date then it is a matter of comparing two dates (as numbers since the epoch ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)

    Get the date parsed properly then you can use the values. Here I use both the .hide() and the .toggle(bool) from jQuery – so with .toggle we can even avoid the conditional wrap and just hide it with a "false" value. ref: http://api.jquery.com/toggle/#toggle-display

    const nowdate = Date.now(); // make this a number to be consistent
    const timeout = 'Fri Jun 17 2022 11:27:28 PST+0100';
    let myTime = Date.parse(timeout);
    console.log('It is a number:', myTime);
    const optionsME = {
      timeZone: 'PST'
    };
    console.log(nowdate, nowdate, myTime, nowdate >= myTime, nowdate - myTime);
    console.log(new Date(nowdate).toLocaleString(undefined, optionsME));
    console.log(new Date(myTime).toLocaleString(undefined, optionsME));
    //use < to make simple bool false to toggle:
    $('.toggle-test').toggle(nowdate < myTime);
    
    if (nowdate >= myTime) {
      console.log(nowdate, myTime, nowdate >= myTime, myTime - nowdate);
      $('.text-test').hide();
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    
    <div class="text-test">tester out</div>
    <div class="toggle-test">toggler out</div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search