Skip to content

Instantly share code, notes, and snippets.

@sclarson
Created June 10, 2014 15:54
Show Gist options
  • Save sclarson/1b8523540c1bffaa517f to your computer and use it in GitHub Desktop.
Save sclarson/1b8523540c1bffaa517f to your computer and use it in GitHub Desktop.
Sometimes JS is beautiful, sometimes it is this.
function determineDateInYesMoveAndUpperCaseFirstWord(yesMove) {
var retVal = yesMove;
if (yesMove.charAt(0) != null
&& !isNaN(parseInt(yesMove.charAt(0),10))) {
var parsedTime;
if (yesMove.indexOf('/') > 0) {
if (yesMove.split('/').length < 3) { // split is one higher than the count of things split on
parsedTime = new moment(yesMove, 'MM/DD');
} else {
if (yesMove.split('/')[2].trim().length < 3) { // if the format has only two days on the year
parsedTime = new moment(yesMove, 'MM/DD/YY');
} else {
parsedTime = new moment(yesMove, 'MM/DD/YYYY');
}
}
} else {
if (yesMove.indexOf('-') > 0) {
if (yesMove.split('-').length < 3) {
parsedTime = new moment(yesMove, 'MM-DD');
} else {
if (yesMove.split('-')[2].trim().length < 3) { // if the format has only two days on the year
parsedTime = new moment(yesMove, 'MM-DD-YY');
} else {
parsedTime = new moment(yesMove, 'MM-DD-YYYY');
}
}
}
}
retVal = parsedTime.format('MM/DD/YYYY');
} else {
var spaceSplit = retVal.split(' ').length;
if (spaceSplit.length > 1) {
spaceSplit[0] = spaceSplit[0].toUpperCase();
retVal = spaceSplit.join(' ');
} else {
retVal = retVal.toUpperCase();
}
}
return retVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment