MooTools 1.5.1 - Parse a string to date respecting a format
Only handles %Y, %y, %m, %d, %H and %i so far.
by Andreas Burg
HTML
<!--
MooTools 1.5.1 - Parse a string to date respecting a format.
Only handles %Y, %y, %m, %d, %H and %i so far.
-->
<pre id="output"></pre>
JavaScript
var
output = $('output'),
format = '%y-%d-%m',
reF = /(.*?%(.{1}).*?)/gi,
arrFormat = format.replace(reF, '$2').split(''),
date = '15-11-12 23:30',
reD = /(.*?(\d{1,4}).*?)/gi,
arrDate = date.replace(reD, '$2-').split('-'),
objDate = {}, dateUTC, dateOut, strOut
;
console.clear();
console.log(arrFormat);
strOut = 'format \t\t=> ' + format + '\n';
strOut += 'arrFormat \t=> ' + arrFormat + '\n';
console.log(arrDate);
strOut += 'date \t\t=> ' + date + '\n';
strOut += 'arrDate \t=> ' + arrDate + '\n';
Array.each(arrFormat, function(item, index){
objDate[item] = parseInt(arrDate[index]);
});
if(!objDate['Y']) {
objDate['Y'] = objDate['y'] >= 70 ? objDate['y'] + 1900 : objDate['y'] + 2000;
}
console.dir(objDate);
strOut += 'objDate \t=> ' + objDate + '\n';
Object.each(objDate, function(item, key){
strOut += '\t\t' + key + ' => ' + item + '\n';
});
dateUTC = objDate['Y'] + '-' + objDate['m'] + '-' + objDate['d'] + 'T' + (objDate['H'] || 0) + ':' + (objDate['i'] || 0);
console.log(dateUTC);
strOut += 'dateUTC \t=> ' + dateUTC + '\n';
dateOut = Date.parse(dateUTC).format('%Y %d %B');
console.log(dateOut);
strOut += 'dateOut \t=> ' + dateOut + '\n';
output.set('text', strOut);