Reputation: 7856
I would like to build an input date in html with automatic formatting. For example, if I write '1 dec 1994' or '12/1/1994' it will automatically detect that it's a date and will reformat it: '12.1.1994' and create a date object: exactly what does excel.
I don't want to build it myself because it seems too complicated and there are too many options so do you know any solution?
Upvotes: 0
Views: 1772
Reputation: 469
Here is complex function, which should convert many common date strings to day, month and year integers. This definitely has some weak spots, so fell free to comment any errors or ideas for upgrade.
The idea behind this script was:
Code:
var el = document.getElementById("date_input"),
reg_day = /\d{1,2}(st|nd|rd|th)/i,
reg_month = /(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i.
reg_year = /^\d{4}$/;
This function adds leading zero (if necessary) to day or month integer (returns string)
Code:
function addZero(num) {
var s = num.toString();
while (s.length < 2) s = "0" + s;
return s;
}
Code:
function getMonth(month) {
if(month.search(reg_month) > -1) {
var month_name = month.match(reg_month)[0];
return new Date(Date.parse(month_name + " 1, 2000")).getMonth() + 1;
} else if(month.search(/^(0?[1-9]|1[0-2])$/) > -1) {
return parseInt(month);
}
return null;
}
Code:
function getDay(day) {
day = day.replace(/\D+/g, "")
if(day.search(/^(0?[1-9]|[12][0-9]|3[01])$/) > -1) {
return parseInt(day);
}
return null;
}
Code:
function getYear(year) {
if(year.search(reg_year) > -1) {
return parseInt(year);
}
return null;
}
Code:
el.onblur = function(){
var val = el.value,
array = val.replace(/[^A-Za-z0-9]/g, " ").replace(/ +(?= )/g, "").split(" "),
data_not_used = [],
day = null, month = null, year = null;
// fn continues below
Else add array index to data_not_used
(more in variables section and in getDay, getMonth and getYear functions)
Code:
// fn continues above
for(var i=0; i < array.length; i++) {
if(array[i].search(reg_day) > -1) {
day = getDay(array[i]);
} else if(array[i].search(reg_month) > -1) {
month = getMonth(array[i]);
} else if(array[i].search(reg_year) > -1) {
year = getYear(array[i]);
} else {
data_not_used.push(i);
}
}
// fn continues below
Code:
// fn continues above
if(data_not_used.length > 1) {
if(array[0].search(/\d{4}/) > -1) {
year = getYear(array[0]);
month = getMonth(array[1]);
day = getDay(array[2]);
} else {
month = getMonth(array[0]);
day = getDay(array[1]);
year = getYear(array[2]);
}
}
else if(data_not_used.length === 1) {
var data = array[data_not_used[0]];
if(day === null) day = getDay(data);
else if(month === null) month = getMonth(data);
else if(year === null) year = getYear(data);
}
// fn continues below
Code:
// fn continues above
if(day!==null && month!==null && year!==null) {
console.log(addZero(month) + "/" + addZero(day) + "/" + year);
} else {
console.error("Date not valid: " + month + "/" + day + "/" + year);
}
};
Don't forget to close onblur() function! Hopefully this was helpful ;)
Upvotes: 2