Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set a start date from the previous month 1

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
I have a end date of 11/6/2009, I need set the start date to 10/31/2009.

Here is my code.

Code:
var endDate = new Date("11/06/2009");
var startDate = new Date();
startDate.setDate(today.getDate()-6)

This works great.

If I set my endDate to "10/30/2009", then I would need my start date to be "10/24/2009"

Code:
var endDate = new Date("10/30/2009");
var startDate = new Date();
startDate.setDate(today.getDate()-6)
but I am getting "11/24/2009", current month and current year? How do I subtract a date in JS and get the correct date back? I have tried google and search's here but would up down dead ends.

Any help is appreciated.

Thanks,
RalphTrent
 
I normally convert the original date into milliseconds using the "getTime()" method, subtract the number of millis in the number of days I want to use, and then convert back into a new Date object by passing the result into the Date constructor.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks BillyRay, that worked out great. It stinks that is this complicated but as long as there is an easy solution, it works.

Thanks again.
 
Date manipulation doesn't have to be complex, though...

With here's the sort of thing you can do:

Code:
// What date is next thrusday?
Date.today().next().thursday();
 
// Add 3 days to Today
Date.today().add(3).days();
 
// Is today Friday?
Date.today().is().friday();
 
// Number fun
(3).days().ago();

// 6 months from now
var n = 6;
n.months().fromNow();
 
// Set to 8:30 AM on the 15th day of the month
Date.today().set({ day: 15, hour: 8, minute: 30 });
 
// Convert text into Date
Date.parse(‘today’);
Date.parse(‘t + 5 d’); // today + 5 days
Date.parse(‘next thursday’);
Date.parse(‘February 20th 1973?);
Date.parse(‘Thu, 1 July 2004 22:30:00?);

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top