tags:

views:

89

answers:

2

I want to add 'Last Year' option in daterangepicker.jQuery.js which will show me date start from- 1/1/2009 to 12/1/2009 (mm/dd/yyyy). how can i add this functionality in js file.

A: 

The daterangepicker have a presetRanges object you can dynamically set.

In your case for example:

$('input').daterangepicker( {
 presetRanges: [
  {text: 'last year', dateStart: '01.01.09', dateEnd: '01.01.10' }
  ]
} );

Obviously, you can use any Date. you can also use Date.js or javascript Date() to dynamically get the dates.

With Date.js it is super easy: http://code.google.com/p/datejs/ (see the "Syntax Overview").

yn2
we can put this in daterangepicker.jQuery.js but my problem is i want date start and dateEnd dynamical, every year i cant change my code
Pankaj
@Pankaj - checkout my answer to achieve what you want.
Krunal
@Pankaj, Krunal Mevada answer is just what you need!
yn2
+2  A: 

Solution:

Just add following preset range code to your daterangepicker js.

presetRanges: [
{ text: 'Last Year', dateStart: function() { var x = Date.parse('1 year ago'); x.setMonth(0); x.setDate(1); return x; }, dateEnd: function() { var x = Date.parse('1 year ago'); x.setMonth(11); return x.moveToLastDayOfMonth(); }}
]
Krunal
BTW, it's an awesome plugin, i have used for report filtering and it rocks !
Krunal
thanks @@krunal
Pankaj