最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Calculate Date - Stack Overflow

programmeradmin0浏览0评论

Any reason why this is landing on the wrong day?

/

I've written the code below for reference, but it's obviously clearer on jsfiddle

<p>Sun / Closed</p>
<p>Mon / Closed</p>
<p>Tues / 9am – 5pm</p>
<p>Wed / 9am – 5pm</p>
<p>Thurs / 9am – 8pm</p>
<p>Fri / 9.30pm – 6.30pm</p>
<p>Sat / 8.30am – 4.30pm</p>

<script>
// Day where 0 is Sunday
var date = new Date();
var d = (date.getDay());

$(function(){
    if (d = 1) {
        $('p:contains("Mon")').addClass('today');
    } else if (d = 2) {
        $('p:contains("Tues")').addClass('today');
    } else if (d = 3) {
        $('p:contains("Wed")').addClass('today');
    } else if (d = 4) {
        $('p:contains("Thurs")').addClass('today');
    } else if (d = 5) {
        $('p:contains("Fri")').addClass('today');
    } else if (d = 6) {
        $('p:contains("Sat")').addClass('today');
    } else {
        $('p:contains("Sun")').addClass('today');
    }    
});
</script>

Any reason why this is landing on the wrong day?

http://jsfiddle/SparrwHawk/MH5wP/

I've written the code below for reference, but it's obviously clearer on jsfiddle

<p>Sun / Closed</p>
<p>Mon / Closed</p>
<p>Tues / 9am – 5pm</p>
<p>Wed / 9am – 5pm</p>
<p>Thurs / 9am – 8pm</p>
<p>Fri / 9.30pm – 6.30pm</p>
<p>Sat / 8.30am – 4.30pm</p>

<script>
// Day where 0 is Sunday
var date = new Date();
var d = (date.getDay());

$(function(){
    if (d = 1) {
        $('p:contains("Mon")').addClass('today');
    } else if (d = 2) {
        $('p:contains("Tues")').addClass('today');
    } else if (d = 3) {
        $('p:contains("Wed")').addClass('today');
    } else if (d = 4) {
        $('p:contains("Thurs")').addClass('today');
    } else if (d = 5) {
        $('p:contains("Fri")').addClass('today');
    } else if (d = 6) {
        $('p:contains("Sat")').addClass('today');
    } else {
        $('p:contains("Sun")').addClass('today');
    }    
});
</script>
Share Improve this question asked Oct 4, 2011 at 0:08 SparrwHawkSparrwHawk 14.1k22 gold badges63 silver badges92 bronze badges 2
  • Not really an answer, but you could simplify your code by doing something like $('p').eq(d-1).addClass('today') – timrwood Commented Oct 4, 2011 at 0:16
  • Ah yes I should have realised that - in the UK it's now Tuesday - and if you put alert(d) it'll spit out '2', but it's still highlighting Monday - wait 'till tomorrow, you'll see ;-) – SparrwHawk Commented Oct 4, 2011 at 0:16
Add a ment  | 

3 Answers 3

Reset to default 7

You gotta use double equal signs (==) to check your value of d, otherwise you'll set d=1 with your first if statement and you'll always have a red Monday.

You are using assignement (=) instead of parison (== or ===) in your if.

So what you are doing is if ( (d = 1) === true ) aka if if you can assign 1 to d. This works, so the code enters your first if and the elses are never touched.

A simple way to make sure not to do that sort of mistake is to reverse the order of the operands when checking against hardcoded values:

$(function(){
  if (1 == d) {
    // do something ...

This way if you mistakenly use =, the assignation fails and you get an error.

You should be able to do something much simpler, like:

$(function()
{
  var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  var day = days[(new Date()).getDay()];
  $('p:contains("' + day + '")').addClass('today');
});
发布评论

评论列表(0)

  1. 暂无评论