Skip to content

Instantly share code, notes, and snippets.

@salsalabs salsalabs/parse_date.html
Last active Aug 29, 2015

Embed
What would you like to do?
Parse normal-looking dates (like 4/5/67) and format them so that Cosm will accept them. The script finds all of the Salsa date fields and adds an onBlur handler to parse and format. Invalid dates are left empty. Doing this relieves the supporter of trying to remember to put in YYYY-MM-DD. This script uses moment.js (http://momentjs.com) by loadi…
<script>
// Script to parse Salsa date fields. This handles the case
// where customers want to use a natural format (4/5/67). Without
// this, error messages can hang stuff up.
// @dependencies http://momentjs.com/ (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js)
//
$(document).ready(function() {
var cdnUrl = 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js';
function setup() {
$('.salsa_date_input').blur(function() {
var text = $(this).val();
if (text.length == 0) { return; }
var date = moment(text).format('YYYY-MM-DD')
date = date == 'Invalid date' ? '' : date;
$(this).val(date);
})
}
if (typeof(moment) == 'undefined') {
$.getScript( cdnUrl, function( data, textStatus, jqxhr ) { setup() });
} else {
setup();
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.