Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Forked from pwim/dateinput_converter.js
Created October 20, 2011 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyhunak/1301808 to your computer and use it in GitHub Desktop.
Save seyhunak/1301808 to your computer and use it in GitHub Desktop.
Convert Rails style date input to jQuery Tools' dateinput
// Based on http://snipt.net/boriscy/datetime-jquery-formtastic/
$.tools.dateinput.localize("ja", {
months: '1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月',
shortMonths: '1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月',
days: '日曜日,月曜日,火曜日,水曜日,木曜日,金曜日,土曜日',
shortDays: '日,月,火,水,木,金,土'
});
$.tools.dateinput.conf.format = 'yyyy-mm-dd';
$(document).ready(function() {
$.tools.dateinput.conf.lang = $('html').attr('lang');
$('div.date, div.datetime').each(function(i, el) {
var sels = $(el).find("select:lt(3)");
var d = new Date(sels[0].value, parseInt(sels[1].value) - 1, sels[2].value);
var dateinput = $("<input type='date'>").dateinput({ value: d} );
// Without this, the field is initially blank
dateinput.val(dateinput.data().dateinput.getValue($.tools.dateinput.conf.format));
dateinput.change(function(event, date) {
$(sels[0]).val(date.getFullYear());
$(sels[1]).val(date.getMonth() + 1);
$(sels[2]).val(date.getDate());
});
$(sels[0]).before(dateinput);
$(sels).hide();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment