views:

39

answers:

3

Hello there,

Can someone guide me on date range in JavaScript ? I want to calculate one week and month date range from current(today's) date i.e if today is 18th july 2010, range for week should be 11/07/2010 - 8/07/2010 and for month it should be 01/07/2010 - 18/07/2010.

Thanks for your guidance in advance.

~Bharat

A: 

I would recommend you looking at the excellent datejs library which has many useful functions to manipulate dates.

Darin Dimitrov
+3  A: 

Try this:

var now = new Date();
var nextWeek = new Date(new Date(now).setDate(now.getDate() + 7));
var nextMonth = new Date(new Date(now).setMonth(now.getMonth() + 1));
no
I'm not sure if it's reliable to add dates like that if say today's date was 25th. It might work on some browsers, but might on others.
Anurag
The Date.prototype.set* functions overflow safely like this according to spec and in all implementations I've ever seen. For example `setDate` from ECMA-262 v.3 (15.9.5.36, p.128) essentially says turn the number of days into a number of seconds and adjust the original date by that number of seconds... so if there are more days than there are in the current month, the month will roll over...
no
thanks @no, I'd wrongly assumed that overflow will not be handled properly on each browser, and always resorted to manual timestamp conversions, so this is good to know.
Anurag
A: 

Hi All thanks for the replies.

Darin link provided by you is fantastic !!!!

Thanks you very much Darin :)

~Bharat

Bharat