Pregunta

Just started using JQuery recently. (Fairly recently, I suppose...) What am I doing wrong here?

var userDate = new Date();
if(userDate.getHours() => 12)
{
    var post = $('p[title="test"]');
    post.text('Would you look at the time?');
}
¿Fue útil?

Solución

The condition is flipped.

   var userDate = new Date();
    if(userDate.getHours() >= 12)
    {
        var post = $('p[title="test"]');
        post.text('Would you look at the time?');
    }

Otros consejos

You probably need to use >= instead of =>, wrong sequence of greater or equal operator. The equal to = comes after > then in >= comparison operator. You can read more about comparion operators here.

var userDate = new Date();
if(userDate.getHours() >= 12)
{
    var post = $('p[title="test"]');
    post.text('Would you look at the time?');
}

Greater than or equal >= Returns true if the left operand is greater than or equal to the right operand, reference.

 if(userDate.getHours() => 12) {...}

Should be

 if(userDate.getHours() >= 12) {...}

The wrong way round maybe?

I must admit sometimes I get confused with the less than or equal to or greater than or equal to.

less than or equal to is

<=

greater than or equal to is

>=

But, if you can't remember these and your code isn't working it's always worth to simplify it buy doing something like this.

var userDate = new Date();
if(userDate.getHours() > 11){
    var post = $('p[title="test"]');
    post.text('Would you look at the time?');
}

This means

Greater than eleven

This is the same but I think it's easier to understand, it must be 12 or greater

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top