10

We are using AngularJS to set filters. Basically we have a start date and end date and frequency. When the frequency is set to week, we simply want to add 1 week to the start date, when the frequency is set to daily, we want to add 1 day to the start date.

Basically something like :-

var date = new date();

date.addDays(2);
date.addMonths(2);
date.addYears(2);
Simon
  • 1,412
  • 4
  • 20
  • 48
  • I'm not familiar with AngularJS but my favorite date library is http://momentjs.com/ . Using that, you should be able to add days very easily with the syntax: moment().add('days', 7); – RoboKozo Jul 17 '14 at 14:50
  • this is a basic javscript date issue and not hard to research – charlietfl Jul 17 '14 at 14:50
  • Please [read the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for the Date object. – m.casey Jul 17 '14 at 14:54
  • It maybe a duplication of a Javascript question, and i realise Angular is javascript, but the question was directed at AngularJS in particular. if there are any functions/methods available within the AngualrJS frame work in which this can be accomplished easily – Simon Jul 18 '14 at 13:19
  • What makes you think Angular has anything to do with dates? – kapa Jul 24 '14 at 15:41
  • @kapa nothing at all, the question was merely inquiring if angualarJS has any inbuilt functions which handle this, as per the .net framework does. however, it seems that it does not. thanks – Simon Aug 04 '14 at 10:07

1 Answers1

15

I would consider using moment.js for all of your JS date related needs then you can do:

var date = moment();
date.add(2, 'days');
date.add(2, 'months');
date.add(2, 'years');

// or all of the above with:
date.add({years: 2, months: 2, days: 2});

And if you need a regular JS date object at the end, check out this post

Community
  • 1
  • 1
Brocco
  • 62,737
  • 12
  • 70
  • 76