This code allows you to set the value of a date_select or datetime_select to the current date or date+time. Stick it in your application.js and then add a new element to your HTML, something like this: <a href="javascript: DateEdit.set_now('post', 'pub_date');">now</a>
var DateEdit = {
Year : 1,
Month : 2,
Day : 3,
Hour : 4,
Minute : 5,
edit_element : function(element_type, model, attribute)
{
return document.getElementById(
model + '_' + attribute + '_' + element_type.toString() + 'i'
);
},
set_value : function(element_type, model, attribute, value)
{
DateEdit.edit_element(element_type, model, attribute).selectedIndex = value;
},
set_year : function(model, attribute, year)
{
year_element = DateEdit.edit_element(DateEdit.Year, model, attribute)
for (i = 0; i < year_element.length; i++)
{
if (year_element.options[i].text == year)
{
year_element.selectedIndex = i;
break;
}
}
},
set_today : function(model, attribute)
{
var now = new Date();
DateEdit.set_year(model, attribute, now.getFullYear());
DateEdit.set_value(DateEdit.Month, model, attribute, now.getMonth());
DateEdit.set_value(DateEdit.Day, model, attribute, now.getDate() - 1);
},
set_now : function(model, attribute)
{
var now = new Date();
DateEdit.set_year(model, attribute, now.getFullYear());
DateEdit.set_value(DateEdit.Month, model, attribute, now.getMonth());
DateEdit.set_value(DateEdit.Day, model, attribute, now.getDate() - 1);
DateEdit.set_value(DateEdit.Hour, model, attribute, now.getHours());
DateEdit.set_value(DateEdit.Minute, model, attribute, now.getMinutes());
}
}