IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

forme date calendrier


Sujet :

JavaScript

  1. #1
    Membre actif Avatar de janyoura
    Femme Profil pro
    étudiante ingénierie informatique
    Inscrit en
    Mars 2012
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : étudiante ingénierie informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 365
    Points : 279
    Points
    279
    Par défaut forme date calendrier
    salut
    j'ai essayé de modifier le code si dessous de façon à obtenir une date sous la forme: YYYY-MM-DD (year-month-day) mais je ne suis pas arrivé à le faire. pouvez vous m'aidez? et merci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    <script type='text/javascript'>
    function show_calendar(str_target, str_datetime) {
       var arr_months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
          "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
       var week_days = ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"];
       var n_weekstart = 1; // day week starts from (normally 0 or 1)
     
    // Si la date est invalide : Initialisation avec la date du jour
    var test_date = /^(\d+)\/(\d+)\/(\d+)$/;
    if (!test_date.exec(str_datetime))
    {
    var d = new Date();
     
    var str_datetime = d.getDate()+ "/" +(d.getMonth() + 1) + "/"+  d.getYear();
     
    }
     
       var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
     
       var dt_prev_month = new Date(dt_datetime);
       dt_prev_month.setMonth(dt_datetime.getMonth()-1);
       var dt_next_month = new Date(dt_datetime);
       dt_next_month.setMonth(dt_datetime.getMonth()+1);
     
       var dt_prev_year = new Date(dt_datetime);
       dt_prev_year.setFullYear(dt_datetime.getFullYear()-1);
       var dt_next_year = new Date(dt_datetime);
       dt_next_year.setFullYear(dt_datetime.getFullYear()+1);
     
       var dt_firstday = new Date(dt_datetime);
       dt_firstday.setDate(1);
       dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
       var dt_lastday = new Date(dt_next_month);
       dt_lastday.setDate(0);
       var title_calendar=(str_target=='dd')?'DATE D\'ARRIVEE':'DATE DE DEPART';
     
       // html generation (feel free to tune it for your particular application)
       // print calendar header
       var StringYear=dt_datetime.getFullYear().toString().substring(dt_datetime.getFullYear().toString().length-2,dt_datetime.getFullYear().toString().length)
       var str_buffer = new String (
          "<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
          "<tr>\n<td bgcolor=\"#4682B4\">"+
          "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
     
    	//ENTETE TABLEAU
    	"<tr>\n   <td bgcolor=\"blue\" style='text-align:center;color:white;font-family:verdana;font-weight:bold;font-size:11px;' colspan=\"6\">"+title_calendar+"<\/td>\n"+
    	"<td bgcolor=\"blue\" style=\"text-align:center;color:navy;font-family:verdana;font-weight:bold;font-size:10px;background-color:silver;border:outset 2px white;cursor:pointer;\" onclick=\"document.getElementById('DivCalendar').style.display='none';\">X<\/td>\n "+
    	"<\/tr>\n"+
     
     
     
    // Ligne Année précédente / suivante
          "<tr>\n   <td bgcolor=\"blue\"><a href=\"javascript:show_calendar('"+
          str_target+"', '"+ dt2dtstr(dt_prev_year)+"');\">"+
          "<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Année précédente\"></a></td>\n"+
          "   <td align=\"center\" bgcolor=\"blue\" colspan=\"5\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
          +" 20"+StringYear+"</font></td>\n"+
          "   <td bgcolor=\"blue\" align=\"right\"><a href=\"javascript:show_calendar('"
          +str_target+"', '"+dt2dtstr(dt_next_year)+"');\">"+
          "<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
     
          " alt=\"Année suivante\"></a></td>\n</tr>\n"+
     
    // Ligne Mois précédent / suivant
          "<tr>\n   <td bgcolor=\"#4682B4\"><a href=\"javascript:show_calendar('"+
          str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
          "<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Mois précédent\"></a></td>\n"+
          "   <td align=\"center\" bgcolor=\"#4682B4\" colspan=\"5\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
          +arr_months[dt_datetime.getMonth()]+" 20"+StringYear+"</font></td>\n"+
          "   <td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:show_calendar('"
          +str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
          "<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Mois suivant\"></a></td>\n</tr>\n"
     
       ); //end newstring
     
       var dt_current_day = new Date(dt_firstday);
     
       // print weekdays titles
       str_buffer += "<tr>\n";
       for (var n=0; n<7; n++)
          str_buffer += "   <td bgcolor=\"#87CEFA\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
          week_days[(n_weekstart+n)%7]+"</font></td>\n";
       // print calendar table
       str_buffer += "</tr>\n";
       while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
          dt_current_day.getMonth() == dt_firstday.getMonth()) {
          // print row header
          str_buffer += "<tr>\n";
          for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
                if (dt_current_day.getDate() == dt_datetime.getDate() &&
                   dt_current_day.getMonth() == dt_datetime.getMonth())
                   // print current date
                   str_buffer += "   <td bgcolor=\"#FFB6C1\" align=\"right\">";
                else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                   // weekend days
                   str_buffer += "   <td bgcolor=\"#DBEAF5\" align=\"right\">";
                else
                   // print working days of current month
                   str_buffer += "   <td bgcolor=\"white\" align=\"right\">";
     
                if (dt_current_day.getMonth() == dt_datetime.getMonth())
                   // print days of current month
                   str_buffer += "<a style=\"cursor:pointer;\" onclick=\"document.getElementById('"+str_target+"').value='"+dt2dtstr(dt_current_day)+"'; document.getElementById('DivCalendar').style.display='none';\">"+
     
                   "<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
                else
                   // print days of other months
     
     
                   str_buffer += "<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
                str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
     
                dt_current_day.setDate(dt_current_day.getDate()+1);
          }
          // print row footer
          str_buffer += "</tr>\n";
       }
       // print calendar footer
       str_buffer +=  "</tr>\n</td>\n</table>\n" ;
     
     
     
       fenCalendrier=document.getElementById('DivCalendar');
       fenCalendrier.innerHTML=str_buffer;
       fenCalendrier.style.top=document.getElementById(str_target).offsetTop+"px";
       fenCalendrier.style.left=Number(document.getElementById(str_target).offsetLeft)+Number(document.getElementById(str_target).offsetWidth)+"px";
       fenCalendrier.style.display="block"
     
    }
    // datetime parsing and formatting routimes. modify them if you wish other datetime format
    function str2dt (str_datetime) {
       var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
       if (!re_date.exec(str_datetime))
          return alert("Invalid Datetime format: "+ str_datetime);
       return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
    }
     
     
     
    /***********************************
    * Formatage de date pour affichage *
    ***********************************/
     
    function dt2dtstr (dt_datetime) {
       var FormatedDate=""
     
       FormatedDate+=(dt_datetime.getDate().toString().length==1)?"0"+dt_datetime.getDate().toString():dt_datetime.getDate().toString();
       FormatedDate+="/";
     	FormatedDate+=((dt_datetime.getMonth()+1).toString().length==1)?"0"+(dt_datetime.getMonth()+1).toString():(dt_datetime.getMonth()+1).toString();
    	FormatedDate+="/";
    	FormatedDate+="20"+dt_datetime.getFullYear().toString().substring(dt_datetime.getFullYear().toString().length-2,dt_datetime.getFullYear()).toString()
     
    	return FormatedDate;
    }
     
    function dt2tmstr (dt_datetime) {
       return (new String (
             dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
    }

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Après une lecture diagonale de ton code
    /***********************************
    * Formatage de date pour affichage *
    ***********************************/

    function dt2dtstr (dt_datetime)
    Tu dois simplement permuter les ordres de la concaténation dans cette fonction.

    A+.

  3. #3
    Membre actif Avatar de janyoura
    Femme Profil pro
    étudiante ingénierie informatique
    Inscrit en
    Mars 2012
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : étudiante ingénierie informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 365
    Points : 279
    Points
    279
    Par défaut
    Citation Envoyé par andry.aime Voir le message
    Bonjour,

    Après une lecture diagonale de ton code

    Tu dois simplement permuter les ordres de la concaténation dans cette fonction.

    A+.
    Je l'ai déjà fais et le résultat: l'écriture en ce format s'effectue mais le problème je ne peut plus ni accéder au mois précèdent ni au mois suivant dans le calendrier. que peut être la raison? merci pour votre aide

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    // datetime parsing and formatting routimes. modify them if you wish other datetime format
    function str2dt (str_datetime) {
    Tu dois aussi modifier la pattern et permuter les ordres des valeurs dans la partie d'instanciation de la date dans la fonction.

    A+.

  5. #5
    Membre actif Avatar de janyoura
    Femme Profil pro
    étudiante ingénierie informatique
    Inscrit en
    Mars 2012
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : étudiante ingénierie informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 365
    Points : 279
    Points
    279
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    function str2dt (str_datetime) {
       var re_date = /^(\d+)\-(\d+)\-(\d+)$/;
       if (!re_date.exec(str_datetime))
          return alert("Invalid Datetime format: "+ str_datetime);
       return (new Date (RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
    }
    voila j'ai changé et permuté mais maintenant j'ai ce problème: invalid DateTime Format 31/10/112

  6. #6
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Dans ton pattern tu utilises "-" comme séparateur or que dans la date que tu files tu utilises "/". La date que tu entres aussi est bizarre. En tout cas, il faut que les deux fonctions se correspondent bien sur les formats.

    A+.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. formulaire: date sous forme de calendrier
    Par satktd dans le forum IHM
    Réponses: 5
    Dernier message: 26/08/2008, 14h24
  2. [FORMS] Objet calendrier
    Par sdiack dans le forum Oracle
    Réponses: 1
    Dernier message: 30/11/2005, 09h43
  3. [SQL Date] Calendrier hebdomadaire
    Par jacquesh dans le forum Oracle
    Réponses: 4
    Dernier message: 23/11/2005, 11h23
  4. Réponses: 15
    Dernier message: 16/09/2005, 17h43
  5. [Dates] Calendrier sous format 20000101
    Par nunor dans le forum Langage
    Réponses: 9
    Dernier message: 10/11/2004, 00h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo