The AppGini Blog
A few tips and tricks to make your coding life a tiny bit better.

Set date range for a datetime picker

Purpose of this recipe

You have a datetime field in a table, and you want to limit allowed dates to a specific date range. For example, for the receipt dat/time field below, you want to limit users to specifying a date/time in the current year only.

Example datetime field

Files to edit

hooks/tablename-dv.js

Where tablename is the name of the table containing the datetime picker.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$j(function() {
 	// get the datetime picker -- replace datetime_field with the actual field name
	var dtp = $j('#datetime_field').parents('.input-group').data('DateTimePicker');
  
  // set the max date to end of current year
	dtp.maxDate(moment().endOf('year'));
  
  // and min date to the start of the current year
	dtp.minDate(moment().startOf('year'));
})

Now, when a user opens the date picker and tries to navigate to a date outside current year, it would be disabled:

Disabled dates