Solved

getting the date of monday and sunday of the current week

Posted on 2013-06-12
3
5,243 Views
Last Modified: 2013-07-08
Hello

I don't program in javascript so I was hoping I might be able to get a bit of help on this forum. When a user loads a webpage I need some javascript that will be able to find out the date of the Monday and the Sunday of the current week. So for example today is Wed 12th of June. The javascript would work out that the date of Monday of the c urrent week is the 10th of June and the date of Sunday is 16th June. If the user loaded the page on Monday 10th of June then the javascript would work out that the date of the Monday of the current week is 10th June. If the user loaded the page on Sunday 16th June then the javascript would work out that the Sunday of the current week is 16th June

Thanks a lot
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39241107
Test page : http://jsfiddle.net/6Wdre/1/
<script>
var today = new Date();
var dayOfWeekStartingSundayZeroIndexBased = today.getDay(); // 0 : Sunday ,1 : Monday,2,3,4,5,6 : Saturday
var mondayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+1);
var sundayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+7);
alert( mondayOfWeek );
alert( sundayOfWeek );
</script>

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39241132
function getMonday(d) {
  d = new Date(d);
  var day = d.getDay(),
      diff = d.getDate() - day + (day == 0 ? -6 : 1);
  return new Date(d.setDate(diff));
}

function getSunday) {
  d = new Date(d);
  var day = d.getDay(),
      diff = d.getDate() - day;
  return new Date(d.setDate(diff));
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39241469
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Validating number not work with decimal 4 41
object method as an event handler 8 69
Date Picker is picky (about something) 4 34
Pass a js value to an html form 5 39
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question