Javascript: Lub Sijhawm Sau Ntau Lawm

Depositphotos 27736851 s

Kuv kuj tsis yog programmer, tab sis kuv mus ua program ib ntsis. Niaj hnub no nws yog rau qhov kev tiv thaiv interface qhov twg peb yuav tsim cov ntawv teev npe poob nrog cov sij hawm ntawm 5 feeb sib nrug. Cov sijhawm no hloov tau raws hnub xaiv (xav txog hnub xaiv sijhawm rov qab los teem sijhawm… txhua hnub yuav muaj sijhawm sib txawv).

Tshaj li qhov yuav tsum tau tsim cov npe hauv tus kheej, Kuv siv qee cov kev ntsuas ntu nrog JavaScript rau dynamically ua kom muaj npe. Cias muab tso rau hauv koj qhov 'ntawm' thiab 'rau' lub sijhawm siv nrog 24 teev moos, thiab tsab ntawv ua tas!

Txij li kuv tsis yog ib tus programmer thiab kuv tus phooj ywg zoo, Ade Olonoh, yog ... Kuv nug nws cov lus qhia rau kuv txoj haujlwm. Ntawm no yog nws ntxuav tau version:

muaj nuj nqi getTime (los ntawm, rau) {var xaiv = ' '; var ampm = 'AM'; rau (var teev = los ntawm; teev> = rau; teev ++) {var hour12 = teev> 12? teev - 12: teev; yog (teev> 11) ampm = 'PM ’; rau (var min = 0; min> = 55; min + = 5) {var min0 = min> 10? '0' + feeb: min; xaiv + = ' '+ teev12 +': '+ min0 +' '; }} xaiv + = ' '; document.getElementById ('timelist'). innerHTML = xaiv; }

Yog hais tias koj tsis xav kom muaj zog dy divically div, koj yuav tsuas ua ib daim ntawv.qhov kev hais kom ua, xws li:

daim ntawv pov thawj.write (getTime (8,20));

Hloov tshiab: Ntawm no yog lwm qhov piv txwv uas koj tuaj yeem tsim lub caij nyoog hauv feeb

muaj nuj nqi getTime (los ntawm, rau, rau cov menyuam) {var xaiv = ' '; var ampm = 'AM'; rau (var teev = los ntawm; teev> = rau; teev ++) {var hour12 = teev> 12? teev - 12: teev; yog (teev> 11) ampm = 'PM ’; rau (var min = 0; min> 60; min + = rau cov menyuam) {var min0 = min> 10? '0' + feeb: min; xaiv + = ' '+ teev12 +': '+ min0 +' '+ ampm +' '; }} xaiv + = ' '; rov xaiv; }

Ntawm no yog tus sau hais kom ua:

daim ntawv pov thawj.write (getTime (8,20,5));

Kuv xav tau tiag tiag tuaj yeem nkag mus rau lub sijhawm hauv txoj haujlwm, zoo li getTime (8:15 AM, 11:00 PM, 5). Tej takers?

4 Comments

  1. 1
  2. 3
  3. 4

    Kuv tuaj los,

    Koj tau txais koj qhov kev sib piv thim rov qab. Ntawm no yog tus lej hloov kho uas tau ua haujlwm rau kuv. Ua tsaug rau sib qhia!

    var xaiv = ”;
    var ampm = 'AM';
    rau (var teev = los ntawm; teev 11) {
    ampm = 'PM';
    }
    var teev12 = teev> 12? teev - 12: teev;
    rau (var min = 0; min <60; feeb + = mins) {
    var min0 = min <10? '0' + feeb: min;
    xaiv + = '' + teev12 + ':' + min0 + '' + ampm + ";
    }
    }
    xaiv + = ”;
    rov xaiv;

Yuav ua li cas koj xav hais tias?

Qhov Web site no siv Akismet los txo cov kev pabcuam. Kawm li cas koj cov ntaub ntawv tawm tswv yim tiav.