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 :

Selection de date dans un calendrier


Sujet :

JavaScript

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 113
    Points : 64
    Points
    64
    Par défaut Selection de date dans un calendrier
    Bonjour,

    Je suis à la recherche d'un script javascript permettant de selectionner une date dans un calendrier s'affichant dans un <DIV> (pas en popup).

    J'en ai trouvé pas mal sur les forums javascript mais aucun ne me convient parfaitement. Ou alors ils embarquent un nombre incalculable de dépendances et je n'arrive jamais à les faire fonctionner. Peut être connaissez vous quelque chose de simple : un simple bout de code javascript a insérer dans ma page et c'est bon...

    En fait je cherche quelque chose composé d'un textField à coté duquel se trouve une petite icône qui provoque l'ouverture d'un calendrier lorsqu'on clique dessus.

    Merci

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 637
    Points : 66 658
    Points
    66 658
    Billets dans le blog
    1
    Par défaut
    un calendrier piqué chez Microsoft et modifié afin d'eviter les popups et le rendre compatible ffx ...
    sasn doutes quelques modifs à faire mias la base est là ...
    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
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    <html>
     
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Calendrier SpaceFrog</title>
    <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()));
    } 
     
     
     
    /*************************
    * comparaison des dates  *
    *************************/
    function comparedate(){
     
    var maintenant=new Date()
    maintenant=dt2dtstr(maintenant).split('\/').reverse().join('')
     
     
     
    if(document.getElementById('da').value.length<1){
                                                                alert("Entrez une date d\'arrivee");
                                                                return false;
                                                                }
     
    var date_arrivee=document.getElementById('da').value.split('\/').reverse().join('');
    if(date_arrivee<maintenant){
                                 alert('vous ne pourrez pas arriver avant aujourd\'hui');
                                 return false;
                                 }
     
    if(document.getElementById('da').value.length<1){
                                                     alert("Entrez une date de départ"); 
                                                     return false;
                                                     }
     
    var date_depart=document.getElementById('dd').value.split('\/').reverse().join('');
    if(date_arrivee>date_depart){
                                 alert("Vous devez arriver avant de repartir!"); 
                                 return false;
                                 }
     
     
    else {        
            return true;}
     
    }
    ('votre nom')
    </script>
    </head>
     
    <body >
    <div id="DivCalendar" style="z-index:2; display:none; width:200px;height:235px; overflow:hidden;position:absolute;Top:1px; left:1px;" ></div>
    <form name="calendar" action="javascript:alert('ok')" onsubmit="return comparedate()" method="post">
    <input type='text' name='da' id='da' onfocus="show_calendar('da','jj,mm,aaaa')" style='text-align:right' readonly/><br/>
    <input type='text' name='dd' id='dd' onfocus="show_calendar('dd')" readonly style='text-align:right' />
    <input type='submit' value='envoyer'/>
    </form>
    </body>
     
    </html>
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 113
    Points : 64
    Points
    64
    Par défaut
    Ok merci, ça marche bien.

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    49
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Décembre 2004
    Messages : 49
    Points : 60
    Points
    60
    Par défaut Léger bug !
    Bonjour,

    Aujourd'hui, Lundi 15 Mai 2006, le calendrier croit que l'on est un Samedi !

    Il doit y avoir un p'ti bug dans le calcul du jour de la semaine.

  5. #5
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 637
    Points : 66 658
    Points
    66 658
    Billets dans le blog
    1
    Par défaut
    pas chez moi il me donne bien le lundi 15 ...
    tu es sur de ta date système ? tu es sous quel navigateur ?
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 113
    Points : 64
    Points
    64
    Par défaut
    En fait, un autre petit probleme : La date ne va pas avant l'an 2000. Si on demande l'année d'avant, il nous donne 2099...

  7. #7
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 637
    Points : 66 658
    Points
    66 658
    Billets dans le blog
    1
    Par défaut
    oui normal c'était pour des reservation d'hotel ... alors à moins de remonter dans le temps ...
    mais c'st facilement modifiable dans le code ...

    la ligne concernée est là :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     FormatedDate+="20"+dt_datetime.getFullYear().toString().substring(dt_datetime.getFullYear().toString().length-2,dt_datetime.getFullYear()).toString()
        return FormatedDate;
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Août 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 113
    Points : 64
    Points
    64
    Par défaut
    Ouai y'a une deuxieme ligne a modifier aussi mais je me rappelle plus ou. Ok là je pense avoir un truc qui fonctionne bien. Merci beaucoup.

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

Discussions similaires

  1. Sélection d'une date dans un calendrier
    Par navis84 dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 17/04/2012, 17h12
  2. Mettre select max(date) dans un paramètre
    Par info dans le forum Développement de jobs
    Réponses: 0
    Dernier message: 25/06/2008, 15h41
  3. SELECT INPUT DATE dans Show Detail Item
    Par lido dans le forum JDeveloper
    Réponses: 2
    Dernier message: 15/10/2007, 11h37
  4. [Calendrier] Choisir une date dans un calendrier
    Par yoyothebest dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 3
    Dernier message: 19/05/2006, 10h39
  5. [VBA-E] choisir une date dans un calendrier
    Par yaya54 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 07/03/2006, 09h05

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