DateFormater
by clawfire
HTML
<script src="https://raw.github.com/timrwood/moment/1.7.0/min/moment.min.js"></script>
<link rel="stylesheet" href="http://f.cl.ly/items/1J3b370k2Q1Z3D3Q2K3V/build_standalone.css">
<script src="http://f.cl.ly/items/3B202P0V2U0T3y3t1p0F/bootstrap-datepicker.js"></script>
<p><input type="text" value="2012-02-16" class="datepicker" data-formatin="YYYY-MM-DD" data-formatout="DD/MM/YYYY"/></p>
<p><button>Convert</button>
JavaScript
$('.datepicker').datepicker({
format:'dd/mm/yyyy'
});
function convert(date, formatIn, formatOut) {
if (date!==null && date !==''){
testWrapper = moment(date, formatIn);
return testWrapper.format(formatOut);
}
}
function reformating() {
$('.datepicker').each(function(){
$(this).val(convert($(this).val(), $(this).data('formatout'), $(this).data('formatin')));
return true;
});
}
// init from an external source, like Mysql
// $('input').val(convert('2012-02-16','YYYY-MM-DD','DD/MM/YYYY'));
// init from value the element himself
$('.datepicker').each(function(){
$(this).val(convert($(this).val(), $(this).data('formatin'),$(this).data('formatout')));
});
// convert when action raised, like post a form
$('button').click(function() {
reformating();
});