1

I would like to have an input field with a button next to it.

On the input field I will enter a date like this: 2011-07-08

And when I hit the button it should read the time that has been entered on the input field and subtract it with 3 months and one day.

Is this possible?

Thanks in advance

Dreni
  • 161
  • 1
  • 5
  • 18

4 Answers4

1

Yes. First you read the date and you convert to a date object

var dateString = document.getElementById('id of your field').value,
    date = new Date(dateString);

then you subtract 91 days and output the result

date.setDate(date.getDate() - 91);
alert(date.toString());

Here I assume for simplicity that you actually want 91 days and not 3 months and one day. If you want three months and one day you will do

date.setMonth(date.getMonth() - 3);
date.setDate(date.getDate() - 1);
alert(date.toString());

The Date object will take care of overflows, leap years and everything.

If you want to write it to same field, taking care of zeroes, you can do

function assureTwoDigits(number) {
    if (number > 9) {
        return '-' + number;
    }
    else {
        return '-0' + number;
    }
}

and change the last line to

document.getElementById('id of your field').value = date.getFullYear() + assureToDigits(date.getMonth()) + assureTwoDigits(date.getDate());
Andrea
  • 20,253
  • 23
  • 114
  • 183
  • 1
    Almost the same thing, but in a new object: `b=new Date(a.getFullYear(),a.getMonth()-3,a.getDate()-1);` Also, be aware that the supported format of the string input to `new Date(string)` is depending on the browser. – some Feb 24 '11 at 11:14
  • Thanks for you help but this results in a alert box with the date. All i want is to change the field from one date to another. For Example: 2011-02-24 -->> press button -->> 2010-11-25 – Dreni Feb 24 '11 at 11:19
  • Of course the alert was an example. One you have the string with the date, you can put it wherever you want. In your case you will replace the last line with `document.getElementById('id of your field').value = date.toString();` – Andrea Feb 24 '11 at 11:21
  • Ok, thanks, but now on the field i get this: "Tue Nov 23 2010 01:00:00 GMT+0100" Instead of 2010-11-23 – Dreni Feb 24 '11 at 11:40
  • Well, then you can format it yourself `document.getElementById('id of your field').value = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();` – Andrea Feb 24 '11 at 11:45
  • Wonderful thanks alot, the only problem now is that it gives me error when the day or month is not two-digit. For example if the date is 2011-05-05 it will return 2011-1-4. I would like it to return 2011-01-04 (With zeroes in front of month and date. And thanks again. – Dreni Feb 24 '11 at 12:01
  • I have updated the answer, since your last request would not fit in a comment. – Andrea Feb 24 '11 at 12:43
  • Thanks a million. Great teacher, great support great community. Thanks – Dreni Feb 24 '11 at 12:45
0

You can use Date objects (see here):

  1. extract year, moth and day from the string (using a regular expression or splitting by '-')
  2. buid a new Date object with that data
  3. subtract the date interval
  4. build the string back
Don
  • 16,928
  • 12
  • 63
  • 101
0

The simplest way would be to split it into an array, then use a couple of if/else statements:

var date = (whatever you're pulling the date in as).split('-');
if (date[1] > 3)
   date[1] = date[1] - 3;
else
   date[0] = date[0] - 1;
   var dateOverflow = date[1]-3;
   date[1] = 12 - dateOverflow;

And then the same for the days.

Trezoid
  • 111
  • 2
  • You can be sure you WILL get this wrong on some date. You have to take care of leap years, and remember that they happen every 4 uear, but not at the turn of the century, except every 4 centuries... Luckily Javascript has Date objects who take care of all this stuff for you! – Andrea Feb 24 '11 at 11:18
  • Bah, silly leap years, always messing everything up for everyone. :P You're right though. – Trezoid Feb 24 '11 at 11:20
0

Yes, it's possible and it's the most clean if you can do it without some arcane regex magic. Start by converting the date to a Date object:

// this will get you a date object from the string:
var myDate = new Date("2011-07-08");

// subtract 3 months and 1 day
myDate.setMonth(myDate.getMonth()-3);
myDate.setDay(myDate.getMonth(), myDate.getDay()-1);

// And now you have the day and it will be correct according to the number of days in a month etc
alert(myDate);
arnorhs
  • 10,383
  • 2
  • 35
  • 38