JavaScript code is expected to return days of the current week only, but skips the second day

Related searches

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)

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; }

Date.prototype.getDay(), The getDay() method returns the day of the week for the specified date according Skip to main content; Select language; Skip to search expected output: 2 The second statement below assigns the value 1 to weekday , based on the value of the Date object Xmas95 . Inheritance: Function; Properties. In JavaScript, the first day of the week (0) means "Sunday", even if some countries in the world consider the first day of the week to be "Monday" You can use an array of names, and getDay() to return the weekday as a name:

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.

Relative Formats - Manual, 'noon', The time is set to 12:00:00, "yesterday noon" 'last' space dayname space 'of', Calculates the last week day of the current month. echo date('F jS, Y', strtotime('Monday next week 2012-04-01')); ?> Month skipping has issues with number of days in the month. But with 'last day of +1 month' the result is different: JavaScript Basic: Exercise-1 with Solution. Write a JavaScript program to display the current day and time in the following format. Today is : Tuesday.

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)

Excel formula: Get Monday of the week, To get the Monday of the week (i.e. the beginning of a week) for any given date, you can use the a formula based on the WEEKDAY function. first day of a week is Monday, configured with WEEKDAY's second argument as explained below. If the date is a Wednesday, you need to roll back 2 days, if the date is a Friday,� Improve this sample solution and post your code through Disqus. Previous: Write a JavaScript function to print the contents of the current window. Next: Write a JavaScript function to find the area of a triangle where lengths of the three of its sides are 5, 6, 7.

JavaScript setMonth() Method, break class continue debugger dowhile for forin forof function ifelse return switch throw This method can also be used to set the day of the month. Expected values are 0-11, but other values are allowed: If the month has 31 days: Return Value: A Number, representing the number of milliseconds between the� Definition and Usage. The DAYOFWEEK() function returns the weekday index for a given date (a number from 1 to 7). Note: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday.

The Beginner's Guide to JavaScript Date and Time, This article provides a very high level overview of the JavaScript Date object. Skip to main content Today, we are going to see how to effectively use date and time The following code creates a Date object, and sets the day of the If the date string contains a common format, we can just create the� Contribute your code and comments through Disqus. Previous: Print a floating-point number in a specified format . Next: Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

See the Pen JavaScript - Difference between two dates in days-date-ex- 8 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus. Previous: Write a JavaScript function to test whether a date is a weekend. Next: Write a JavaScript function to get the last day of a month.

Comments
  • if first equals the first day, why add 1 to it?
  • if first is initially 1, first = first + 1 makes first 2, second = first + 2 makes second 4 ... see the error?
  • Thank you, I greatly appreciate your help
  • 12th wasn't Monday. You should increment all days by 1 for a correct answer. But I think, author should be able to figure it out.