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 :

detecter automatiquement le Daylight saving time(dst)


Sujet :

JavaScript

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 64
    Points : 35
    Points
    35
    Par défaut detecter automatiquement le Daylight saving time(dst)
    Bonjour,

    je developpe un site intranet .sur mon site ja affiché l'heure(evolue pas statique) de plusieurs pays(france,uk,boston,london...).pour faire ça j'ai utilisé un script javascript dont le code
    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
     				<script type="text/javascript">
    	var listHtmlElem = [];
    	//listHtmlElem.push([$(DIV_ID),GMT]);
    	listHtmlElem.push([$('LD'),+1]);
    	listHtmlElem.push([$('Bos-Mer'),-4]);
    	listHtmlElem.push([$('Dallas'),-5]);
    	listHtmlElem.push([$('Loc'),+2]);
    	listHtmlElem.push([$('Bangalore'),+7]);
     
    	 function $(id){
    	return document.getElementById(id);
    		}
     
    	 function displayTime(HtmlElem,gmt){
    	var date = new Date();
    	var utc = gmt;
    	var Hour;
     
    	utc *= 60; // minutes
    	utc += date.getTimezoneOffset(); // correction local -> gmt
    	utc *= 60000; // millisecondes
    	Hour=new Date(date.getTime() + utc).toUTCString().replace('UTC','').split(' ');
    	HtmlElem.innerHTML = Hour[4];
    		}
     
    	 function updateTime(){
     
    	for(var i in listHtmlElem)
     
    		displayTime(listHtmlElem[i][0],listHtmlElem[i][1]);
    		}
    	setInterval(updateTime,1000);
     
    </script>
    je veux qu'on detecte automatiquement le Daylight saving time (dst) (ajout ou soustraction d'une heure ou plus dans certains pays).
    peut on changer le code pour faire ça ?
    merci d'avance.

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 64
    Points : 35
    Points
    35
    Par défaut
    s'il vous plait si qq a une ideé comment tenir compte du le Daylight saving time utilisant javascript peut -il- m'aider .
    merci

  3. #3
    Membre confirmé Avatar de nadox
    Homme Profil pro
    Développeur
    Inscrit en
    Février 2010
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2010
    Messages : 360
    Points : 551
    Points
    551

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 64
    Points : 35
    Points
    35
    Par défaut
    Bonjour,
    je vous remerci pour ton aide.j'ai ajouter les deux fonctions dans un meme script mais malheureusement ça marche pas?

  5. #5
    Membre confirmé Avatar de nadox
    Homme Profil pro
    Développeur
    Inscrit en
    Février 2010
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2010
    Messages : 360
    Points : 551
    Points
    551
    Par défaut
    Quelle est la question ? Ça marche pas ? Je ne peux pas deviner le code que tu as sous les yeux...

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 64
    Points : 35
    Points
    35
    Par défaut
    desolé mais je parle des fonctios situé dans le lien que vous m'avez envoyé
    voici le code
    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
     
    <script>
     
    function TimezoneDetect(){ 
        var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear()); 
        var intOffset = 10000; //set initial offset high so it is adjusted on the first attempt 
        var intMonth; 
        var intHoursUtc; 
        var intHours; 
        var intDaysMultiplyBy; 
     
        //go through each month to find the lowest offset to account for DST 
        for (intMonth=0;intMonth < 12;intMonth++){ 
            //go to the next month 
            dtDate.setUTCMonth(dtDate.getUTCMonth() + 1); 
     
            //To ignore daylight saving time look for the lowest offset. 
            //Since, during DST, the clock moves forward, it'll be a bigger number. 
            if (intOffset > (dtDate.getTimezoneOffset() * (-1))){ 
                intOffset = (dtDate.getTimezoneOffset() * (-1)); 
            } 
        } 
     
        return intOffset; 
    }
     
    //Find start and end of DST 
    function DstDetect(){ 
        var dtDstDetect = new Date(); 
        var dtDstStart = ''; 
        var dtDstEnd = ''; 
        var dtDstStartHold = ''; //Temp date hold 
        var intYearDayCount = 732; //366 (include leap year) * 2 (for two years) 
        var intHourOfYear = 1; 
        var intDayOfYear; 
        var intOffset = TimezoneDetect(); //Custom function. Make sure you include it. 
     
        //Start from a year ago to make sure we include any previously starting DST 
        dtDstDetect = new Date() 
        dtDstDetect.setUTCFullYear(dtDstDetect.getUTCFullYear() - 1); 
        dtDstDetect.setUTCHours(0,0,0,0); 
     
        //Going hour by hour through the year will detect DST with shorter code but that could result in 8760 
        //FOR loops and several seconds of script execution time. Longer code narrows this down a little. 
        //Go one day at a time and find out approx time of DST and if there even is DST on this computer. 
        //Also need to make sure we catch the most current start and end cycle. 
        for(intDayOfYear = 1; intDayOfYear <= intYearDayCount; intDayOfYear++){ 
            dtDstDetect.setUTCDate(dtDstDetect.getUTCDate() + 1); 
     
            if ((dtDstDetect.getTimezoneOffset() * (-1)) != intOffset && dtDstStartHold == ''){ 
                dtDstStartHold = new Date(dtDstDetect); 
            } 
            if ((dtDstDetect.getTimezoneOffset() * (-1)) == intOffset && dtDstStartHold != ''){ 
                dtDstStart = new Date(dtDstStartHold); 
                dtDstEnd = new Date(dtDstDetect); 
                dtDstStartHold = ''; 
     
                //DST is being used in this timezone. Narrow the time down to the exact hour the change happens 
                //Remove 48 hours (a few extra to be on safe side) from the start/end date and find the exact change point 
                //Go hour by hour until a change in the timezone offset is detected. 
                dtDstStart.setUTCHours(dtDstStart.getUTCHours() - 48); 
                dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() - 48); 
     
                //First find when DST starts 
                for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){ 
                    dtDstStart.setUTCHours(dtDstStart.getUTCHours() + 1); 
     
                    //If we found it then exit the loop. dtDstStart will have the correct value left in it. 
                    if ((dtDstStart.getTimezoneOffset() * (-1)) != intOffset){ 
                        break; 
                    } 
                } 
     
                //Now find out when DST ends 
                for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){ 
                    dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() + 1); 
     
                    //If we found it then exit the loop. dtDstEnd will have the correct value left in it. 
                    if ((dtDstEnd.getTimezoneOffset() * (-1)) != (intOffset + 60)){ 
                        break; 
                    } 
                } 
     
                //Check if DST is currently on for this time frame. If it is then return these values. 
                //If not then keep going. The function will either return the last values collected 
                //or another value that is currently in effect 
                if ((new Date()).getTime() >= dtDstStart.getTime() && (new Date()).getTime() <= dtDstEnd.getTime()){ 
                    return new Array(dtDstStart,dtDstEnd); 
                } 
     
            } 
        } 
        return new Array(dtDstStart,dtDstEnd); 
    }
    </script>

  7. #7
    Membre confirmé Avatar de nadox
    Homme Profil pro
    Développeur
    Inscrit en
    Février 2010
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2010
    Messages : 360
    Points : 551
    Points
    551
    Par défaut
    A quel endroit tu utilise le code ? Comment ? Il ne suffit pas de copier/coller le script, il faut aussi l'utiliser (dans ton code) !

  8. #8
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2011
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 64
    Points : 35
    Points
    35
    Par défaut
    je l'ai mis dans le body

  9. #9
    Membre confirmé Avatar de nadox
    Homme Profil pro
    Développeur
    Inscrit en
    Février 2010
    Messages
    360
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Développeur
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2010
    Messages : 360
    Points : 551
    Points
    551
    Par défaut
    Tu as mis quoi dans le body ? Juste le code des fonctions ? Ben il ne va pas faire ce que tu veux comme par magie !

    Il faut modifier ton premier code pour qu'il utilise les fonctions. Si tu ne sais pas faire ça, il faut peut-être commencer par apprendre javascript !

Discussions similaires

  1. detecter automatiquement les images stockées
    Par younek dans le forum Débuter avec Java
    Réponses: 1
    Dernier message: 25/05/2008, 21h17
  2. detection automatique de reseau (filaire)
    Par lezurp dans le forum Réseau
    Réponses: 0
    Dernier message: 19/02/2008, 21h12
  3. Detecter automatiquement le lecteur de CD
    Par jean-paul lepetit dans le forum VB.NET
    Réponses: 16
    Dernier message: 17/10/2007, 16h31
  4. Réponses: 4
    Dernier message: 22/11/2006, 13h07
  5. Detection automatique de fonte
    Par HNT dans le forum Général Conception Web
    Réponses: 6
    Dernier message: 16/10/2005, 18h16

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