I have 2 buttons( Previous and next ) for week. In between the previous and next week button needs to show week ending date (Fridays) for that week. I want to display previous week date from Sun 11/18 to Mon 11/26 when we click the previous week button. Same as the next week button click event to show from Mon 11/26" to Sun 12/2. How it possible to show? and how to take the week ending date (Fridays) for that week?
Asked
Active
Viewed 3,048 times
-1
-
1post some codes what you've tried. It'll be helpful to solve – polin Nov 26 '12 at 07:53
-
if you are working with button, why you tag `php`...?? don't you know `php` is a server side language...?? – Naz Nov 26 '12 at 08:23
1 Answers
1
This's to get the date in 7 days, and from 7 days ago:
var today = new Date();
var nextWeek = Date.parse(new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7));
var previousWeek = Date.parse(new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7));
If you don't want a timestamp returned, remove the Date.parse()
from it to receive a string like this:
Mon Dec 03 2012 00:00:00 GMT+0100 (West-Europa (standaardtijd))
(This was returned on a system with Dutch language settings)
To get the week dates, take a look at these 2 questions:
JavaScript - get the first day of the week from current date
How to get first and last day of the week in JavaScript
-
-
-
Okay. Suppose I want the previous week of the date 11 th Nov. The how to take it? – Anu Nov 26 '12 at 08:18
-
Pass the date as [IETF-compliant RFC 2822 timestamp](http://tools.ietf.org/html/rfc2822#page-14) to the [Date object (example)](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Examples) So: `var today = new Date('Nov 11, 2012');`, or, in this format: `new Date(year, month, day [, hour, minute, second, millisecond])` with the parameters between brackets being optional. – Cerbrus Nov 26 '12 at 08:25
-
We get next week like Mon Dec 03 2012 00:00:00 GMT+0530 (IST). Then how to take next 7 days to print like (mon to sun) – Anu Nov 26 '12 at 08:30
-
-
var previousWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7); var previousWeekSplit = previousWeek.split(" "); alert(previousWeekSplit[0]); – Anu Nov 26 '12 at 09:25
-
-
Yes, `previousWeek` is a date object, not a string. You can do: `previousWeek.toString().split(" ");`, or, use one of the `get` methods from the [Date object](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Methods) – Cerbrus Nov 26 '12 at 09:37
-
If u don't mind.. Can u give me ur mail id? So I can ask ma doubts directly :) – Anu Nov 26 '12 at 09:47
-
That would defeat the purpose of StackOverflow as community / support site. – Cerbrus Nov 26 '12 at 09:51