OVH Cloud OVH Cloud

Creation d'objets Date , probleme avec fevrier ?

12 réponses
Avatar
xav
Bonjour,

voila ci joint un bout de code de test, quelqu'un peut il m'expliquer
pourquoi lors de la creation d'une date JS a l'aide des setters ,
l'ordre de definition est important ?

l'exemple suivant, devrait a mon avis retourner 3 fois la meme date dans
tous les cas, et bien non, le 3eme retour annonce 1 mois de plus si on
defini une date en fevrier.
C'est moi qui ai 5 trains de retard ? c'est un bug connu ?

Merci d'avance d'éclairer ma lanterne.

Xavier
--------------------------------------------------------------------------
<html>
<head>
<script language="javascript">

function dateFromString(datestring)
{
var parts = datestring.split('-');
var iday = parts[2];
var imonth = parseInt(parts[1]) - 1;
var iyear = parts[0];

var str = '';

str += "init string date is: " + datestring + "\n\n";
str += "Day is: " + iday + ", jsMonth is: " + imonth + ", Year is:
" + iyear + "\n\n";

str += "Date from constructor: " + new Date(iyear, imonth, iday) +
"\n\n";

var d1 = new Date();
d1.setDate(iday);
d1.setMonth(imonth);
d1.setFullYear(iyear);
str += 'Date 1 ok: ' + d1 + "\n\n";

var d2 = new Date();
d2.setFullYear(iyear);
d2.setMonth(imonth);
d2.setDate(iday);

str += 'Date 2 (error?): ' + d2;

alert(str);
}

</script>
</head>
<body
onload="dateFromString('2007-2-14');dateFromString('2007-2-28');
dateFromString('2006-2-14'); dateFromString('2007-1-12');
dateFromString('2007-11-01'); dateFromString('2006-12-24'); ">
</body>
</html>

2 réponses

1 2
Avatar
Dr John Stockton
JRS: En article <449fd75f$0$868$, datee Mon, 26
Jun 2006 14:47:34 remote, vu en news:fr.comp.lang.javascript, Cenekemoi

function isDateValide(Year,Month,Day) {
var dDate=new Date(Year,Month-1,Day);
return ( dDate.getFullYear()==Year && dDate.getMonth()==Month-1 &&
dDate.getDate()=Úy )
}



function isDateValide(Year, Month, Day) {
var dDate = new Date(Year, Month-1, Day);
return ( dDate.getMonth()==Month-1 && dDate.getDate()=Úy ) }

C'est assez pour chaque annee apres /anno domini/ zero jusqu'a >
A.D.275000.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Avatar
Cenekemoi
JRS: En article <449fd75f$0$868$, datee Mon,
26 Jun 2006 14:47:34 remote, vu en news:fr.comp.lang.javascript,

function isDateValide(Year,Month,Day) {
var dDate=new Date(Year,Month-1,Day);
return ( dDate.getFullYear()==Year && dDate.getMonth()==Month-1
&& dDate.getDate()=Úy )
}



function isDateValide(Year, Month, Day) {
var dDate = new Date(Year, Month-1, Day);
return ( dDate.getMonth()==Month-1 && dDate.getDate()=Úy ) }

C'est assez pour chaque annee apres /anno domini/ zero jusqu'a >
A.D.275000.


Oui, c'est exact mais pas toujours très clair quand on relit le code
(indépendamment du gain -très minime- de cpu)...

--
Cordialement, Thierry ;-)


1 2