"Lasse Reichstein Nielsen" wrote
> Du kan prøve den her, men prøv at skrive reglerne ned for
> hvordan de forskellige strenge skal fortolkes:
>
I første omgang bliver det to cifre for dag samt måned og 4 cifre for årstal
Jeg fant frem til denne efter "lidt" trial'n'error samt hjælp fra
http://www.regular-expressions.info/dates.html
Ikke superperfekt men en start :)
Den tygger sig igennem fx
12
1203
12.03
12/03
12-03
12.03.2006
12/03/2006
12-03-2006
function parseDateAggressively(string) {
var today = new Date();
var year = today.getFullYear();
var month = Number(today.getMonth()) +1; // please note getMonth is zero
based
var date = today.getDate();
// valid input? bail out before it's too late
if (string.length < 2) {
return;
}
// check
http://www.regular-expressions.info/dates.html for more info on
regular expressions
var split = /\b^(0[1-9]|[12][0-9]|3[01])[- /.]?(0[1-9]|1[012])?[-
/.]?((19|20)\d\d|[0-9][0-9])?\b$/.exec(string);
if (split) {
parts = split.slice(1);
}
// get the year if present
if (parts[2] !== undefined) {
year = Number(parts[2]);
if (year < 100) { year += 2000 };
}
// get the month if present
if (parts[1] !== undefined) {
month = Number(parts[1]) ;
}
// get the date assume present
date = Number(parts[0]);
// handle wrong date eg 31/02 - 31/11 etc
// note month is zerobased
var calcDate = Number(new Date(year, month - 1, date).getDate());
if (calcDate !== date) {
// go back to last day of month
date = date - calcDate;
}
// note month is zerobased
return new Date(year, month - 1, date);
}
- Peter