Comment vérifier si un champ qui contient la date et l'heure au format
:arrow: http://javascript.developpez.com/faq...erifFormatDate
- Je remplace les appels à eval() par un parseInt(nombre, 10);. Je précise la base 10 car parseInt() va considérer que si la chaine de caractère débute par un 0, la valeur est en base 8.
J'ai donc remanié le code en ce sens.
- La fonction traite les dates au format américain c'est à dire yyyy-mm-dd hh:mm:ss. Je propose en plus une fonction qui traite les dates au format jj-mm-aaaa hh:mm:ss
- La fonction date.getYear() retourne, selon le navigateur, une année sur 2 ou 4 chiffres. Je l'ai remplacée par getFullYear(), plus fiable à mon avis. Du coup, le test if (an.length<4) an=an+1900; devient inutile.
- J'ai également aéré le code en ajoutant des lignes vides
- Mise à jour 18/11 : groupement des déclarations de variables
Voici ma proposition de correction :
Citation:
Pour analyser une date au format "yyyy-mm-dd hh:mm:ss" (format américain) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
// date au format yyyy-mm-dd hh:mm:ss
function IsGoodDate(mydate)
{
var thedate, year, month, day;
var onedate;
var thetime, hours, minutes, seconds;
var onetime;
var mysplit=mydate.split(' ');
if (mysplit.length != 2)
return false;
thedate=mysplit[0].split('-');
year = parseInt(thedate[0],10);
month = parseInt(thedate[1],10);
day = parseInt(thedate[2],10);
if ((mysplit[0].length != 10) || (thedate.length != 3) ||
(isNaN(year)) || (isNaN(month)) || (isNaN(day)) ||
(thedate[0].length < 4) || (thedate[1].length < 2) || (thedate[2].length < 2))
return false;
onedate = new Date(year, month-1, day);
year = onedate.getFullYear();
if ((onedate.getDate() != day) ||
(onedate.getMonth() != month-1) ||
(onedate.getFullYear() != year ))
return false;
thetime = mysplit[1].split(':');
hours = parseInt(thetime[0],10);
minutes = parseInt(thetime[1],10);
seconds = parseInt(thetime[2],10);
if ((mysplit[1].length != 8) || (thetime.length != 3)||
(isNaN(hours)) || (isNaN(minutes)) || (isNaN(seconds)) ||
(thetime[0].length < 2) || (thetime[1].length < 2) || (thetime[2].length < 2))
return false;
onetime = new Date(year, month-1, day, hours, minutes, seconds);
if ((onetime.getHours() != hours) || (onetime.getMinutes() != minutes) || (onetime.getSeconds() != seconds))
return false;
return true;
} |
Cette fonction teste d'abord si le paramètre "mydate" est bien composé des 2 éléments date et heure séparés par un espace. Ensuite, on teste si la date est bien composée des 3 éléments séparés par des tirets et si les longueurs de ces éléments sont bien 4, 2 et 2. Après ça, la fonction vérifie la validité de la date en la
testant directement dans le calendrier (concordance)... Par exemple, 2004-02-31 retournera false car il n'existe pas de 31 février... Pour l'heure, c'est un peu pareil...
Pour analyser une date au format "jj-mm-aaaa hh:mm:ss" (format français) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
// date au format dd-mm-yyyy hh:mm:ss
function IsGoodDate(mydate)
{
var thedate, day, month, year;
var onedate;
var thetime, hours, minutes, seconds;
var onetime;
var mysplit=mydate.split(' ');
if (mysplit.length != 2)
return false;
thedate=mysplit[0].split('-');
day = parseInt(thedate[0],10);
month = parseInt(thedate[1],10);
year = parseInt(thedate[2],10);
if ((mysplit[0].length != 10) || (thedate.length != 3) ||
(isNaN(year)) || (isNaN(month)) || (isNaN(day)) ||
(thedate[0].length < 2) || (thedate[1].length < 2) || (thedate[2].length < 4))
return false;
onedate = new Date(year, month-1, day);
year = onedate.getFullYear();
if ((onedate.getDate() != day) ||
(onedate.getMonth() != month-1) ||
(onedate.getFullYear() != year ))
return false;
thetime = mysplit[1].split(':');
hours = parseInt(thetime[0],10);
minutes = parseInt(thetime[1],10);
seconds = parseInt(thetime[2],10);
if ((mysplit[1].length != 8) || (thetime.length != 3)||
(isNaN(hours)) || (isNaN(minutes)) || (isNaN(seconds)) ||
(thetime[0].length < 2) || (thetime[1].length < 2) || (thetime[2].length < 2))
return false;
onetime = new Date(year, month-1, day, hours, minutes, seconds);
if ((onetime.getHours() != hours) || (onetime.getMinutes() != minutes) || (onetime.getSeconds() != seconds))
return false;
return true;
} |