0

I am just a newbie. I am trying delete a span when the time is above 6:30 pm daily. Code below:

(function(){
var d = new Date();
d.setHours(18,30,0);
if (d<new Date().toLocaleString());{
 $("span:contains('Material Receive')").remove()
return false;}})();

However it s not working. It is always removing, i.e 24x7.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    There is a semicolon after `if` statement `);{`, is it only in the sample code? – Prasun Oct 06 '18 at 18:14
  • Could you explain what the code is supposed to do? Should it really delete the content *when* its 6:30 or *if* it is 6:30 ? What if the page gets opened at 8pm? Should the message disappear immeadiately or never or the next day at 6:30? Should the message reappear somewhen? – Jonas Wilms Oct 06 '18 at 18:28
  • @Jonas Wilms.. i am working on others Website using bookmarklet via Tampermonkey. the bookmarklet run automatically. i don't know other way to block the user for clicking on Material Receiving Button after 6:30 PM... removing the Button is just fine for me... :) – Sachin Agarwal Oct 06 '18 at 18:37

3 Answers3

0

Try to not compare an object with a string. Use 2 numbers instead. And lose the stray semicolon.

if (d.getTime() < new Date().getTime()){...}
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
0

getHours Method will fit perfect i think

var d = new Date();
if (1830 < d.getHours()+""+d.getMinutes()){
    $("span:contains('Material Receive')").remove()
    return false;
}
Ugur Eren
  • 1,968
  • 2
  • 11
  • 21
0

Above answers already explained for you. Anyway:

Getting current date and time in JavaScript

I didn't understand exactly what you want to do, I suppose to delete a span every day at 18:30, right?

In this case, when you create the date object, you have to access hours and minutes to check time so:

( function() {

    var d = new Date();
    if( ( d.getHours() == 18 ) && ( d.getMinutes() == 30 ) ) {

        $("span:contains('Material Receive')").remove();
        return false; //Useless in a self-invoking function

    }

})();
Fabrizio
  • 233
  • 1
  • 10
  • as i am a new user so cannot Vote :( – Sachin Agarwal Oct 06 '18 at 18:50
  • 1
    It's fine. I'm glad you solved your problem. Anyway, improve your syntax, there were some don't-needed semicolons and that "return false;" is useless since your function is self-invoking ( it means you cannot call it in other ways, so there's no point where your return is used ) and gets a new instance, so the "return" doesn't block any eventually parent function. – Fabrizio Oct 06 '18 at 19:00