tags:

views:

479

answers:

4

I have a text box id="somedate" that has a date value "09/27/2009"

I'm looking for an easy way to subtract 7 days and replace the value to 09/20/2009. then 09/13/2009, etc...at the click of a button

Obviously I need the process to be able to cross back over months and years.

Is there an easy way of doing this in jQuery?

A: 

Not in native JQuery, but there may or may not be libraries which makes it somewhat easier. I'd suggest you take a look at the internals of the JQuery Timepicker plugin (or one of them if there are more than one) and see what they use.

Deniz Dogan
+2  A: 

Aside from getting the content of the input, I don't think jQuery has much to do with your problem.

You should look at the Javascript Date object for date/time calculations.

Kieron
Thanks...makes sense to not always think specific to a language or framework.
Jason
Sometimes it helps stepping back and seeing that's there (:
Kieron
A: 

I don't believe this facility exists in jQuery, but there are libraries like Datejs (clicky) that make managing dates a lot easier. There are really too many pitfalls with date math to try to roll your own.

Date.parse('Sept 30th, 2009, 10:30 AM').addWeeks(-1);
warrenm
A: 

My two cents:

var d = new Date($("#textbox").attr("value"));
var nd = new Date(d - new Date(7*24*60*60*1000));

function f(s) { return s[1] ? s : "0"+s; }; // ensure string is two chars long
$("#textbox").attr("value",
    f((nd.getMonth()+1).toString()) + "/" +
    f(nd.getDate().toString()) + "/" +
    nd.getFullYear());
andre-r