Skip to content

Instantly share code, notes, and snippets.

@ivarvong
Created October 15, 2014 15:44
Show Gist options
  • Save ivarvong/fb1682acd5eec3d78308 to your computer and use it in GitHub Desktop.
Save ivarvong/fb1682acd5eec3d78308 to your computer and use it in GitHub Desktop.
$(document).ready(function() {
$('body').on('click', '.set-to-now', function(e) {
e.preventDefault();
var target_selector = $(this).attr('data-target');
var year = $(target_selector+'_1i');
var month = $(target_selector+'_2i');
var day = $(target_selector+'_3i');
var hour = $(target_selector+'_4i');
var minute = $(target_selector+'_5i');
// var current = [year, month, day, hour, minute].map(function(item) {
// return item.val();
// });
var now = new Date();
year.val( now.getFullYear() );
month.val( now.getMonth()+1 );
day.val( now.getDate() );
hour.val( now.getHours() );
minute.val( now.getMinutes() );
});
});
@ivarvong
Copy link
Author

Assuming your model is "Post" and the form has something like:

<%= f.datetime_select :published_at %> <a href="#" class="set-to-now" data-target="#post_published_at">NOW</a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment