Reputation: 1469
My JavaScript code is expected to return days of the current week only, but skips the second day.
Here is the code:
var curr = new Date;
var first = curr.getDate() - curr.getDay();
var first = first + 1;
var second = first + 2;
var third = first + 3;
var fourth = first + 4;
var fifth = first + 5;
var last = first + 6;
var monday = new Date(curr.setDate(first)).toLocaleDateString();
var tuesday = new Date(curr.setDate(second)).toLocaleDateString();
var wednesday = new Date(curr.setDate(third)).toLocaleDateString();
var thursday = new Date(curr.setDate(fourth)).toLocaleDateString();
var friday = new Date(curr.setDate(fifth)).toLocaleDateString();
var sunday = new Date(curr.setDate(last)).toLocaleDateString();
//alert(monday+" "+sunday)
document.write(monday + " " + tuesday + " " + wednesday + " " + thursday + " " + friday + " " + sunday)
this is my output: 5/13/2019 5/15/2019 5/16/2019 5/17/2019 5/18/2019 5/19/2019 ...notice 5/14/2019 is missing, could I get some help with this please?
Upvotes: 0
Views: 55
Reputation: 22334
as day 'zero' is Sunday You may have :
var curr = new Date;
var zero = curr.getDate() - curr.getDay(); // Sunday - Saturday : 0 - 6
var first = zero + 1;
var second = zero + 2;
var third = zero + 3;
var fourth = zero + 4;
var fifth = zero + 5;
var last = zero + 6;
var monday = new Date(curr.setDate(first)).toLocaleDateString();
var tuesday = new Date(curr.setDate(second)).toLocaleDateString();
var wednesday = new Date(curr.setDate(third)).toLocaleDateString();
var thursday = new Date(curr.setDate(fourth)).toLocaleDateString();
var friday = new Date(curr.setDate(fifth)).toLocaleDateString();
var saturday = new Date(curr.setDate(last)).toLocaleDateString();
document.write( 'monday.....', monday , '<br>' )
document.write( 'tuesday....', tuesday , '<br>' )
document.write( 'wednesday..', wednesday , '<br>' )
document.write( 'thursday...', thursday , '<br>' )
document.write( 'friday.....', friday , '<br>' )
document.write( 'saturday...', saturday , ' ==> ( not sunday ) <br>' )
body { font-family: 'Courier New', Courier, monospace; }
Upvotes: 1
Reputation: 15857
You are adding 1 to your first day, then incrementing from there incorrectly.
var curr = new Date;
var first = curr.getDate() - curr.getDay();
var monday = new Date(curr.setDate(first+1)).toLocaleDateString();
var tuesday = new Date(curr.setDate(first+2)).toLocaleDateString();
var wednesday = new Date(curr.setDate(first+3)).toLocaleDateString();
var thursday = new Date(curr.setDate(first+4)).toLocaleDateString();
var friday = new Date(curr.setDate(first+5)).toLocaleDateString();
var saturday = new Date(curr.setDate(first+6)).toLocaleDateString();
//alert(monday+" "+sunday)
document.write(monday + " " + tuesday + " " + wednesday + " " + thursday + " " + friday + " " + saturday)
Upvotes: 0
Reputation: 9706
You are changing first
, and then start adding to it.
Consider this code:
var n = 0;
var n = n+1;
var n2 = n+2;
you will end up with n == 1
and n2 == 3
.
Also, if you were using let
instead of var
, JavaScript would have caught this error for you.
Upvotes: 0