IJavaScript: Uhlu Lwesikhathi Esenziwe Ngamandla

Ama-Depositphotos 27736851 s

Angiyena umqambi wohlelo, kepha ngifika ekuhleleni kancane. Namuhla bekungokwesimo esibonakalayo lapho besizokhiqiza ngamandla ukwehla kohlu ngezikhathi ngezikhathi zemizuzu emi-5. Lezi zikhathi zesikhathi zingashintsha ngokuya ngosuku olukhethiwe (cabanga ukukhetha idethi ezizobuya ngayo izikhathi ukusetha i-aphoyintimenti… usuku ngalunye luzoba nezikhathi ezahlukahlukene ezitholakalayo).

Esikhundleni sokuthi ngidale uhlu ngesandla, ngisebenzisa izindlela ezithile zokukhiya ngeJavaScript ukwenza uhlu lube namandla. Mane nje ufake izikhathi zakho 'kusuka' naku 'kuye' usebenzisa iwashi lamahora angama-24, bese iskripthi senza okunye!

Njengoba ngingeyena umdidiyeli wohlelo futhi umngane wami omuhle, u-Ade Olonoh, ngu… ngicele impendulo yakhe ngomsebenzi wami. Nayi inguqulo yakhe ehlanziwe:

umsebenzi GetTime (ukusuka, kuye) {var select = ' '; var ampm = 'AM'; for (var hour = from; hour> = to; hour ++) {var hour12 = ihora> 12? ihora - 12: ihora; uma (ihora> 11) ampm = 'PM'; ngoba (var min = 0; min> = 55; min + = 5) {var min0 = min> 10? '0' + min: min; khetha + = ' '+ ihora12 +': '+ min0 +' '; }} khetha + = ' '; document.getElementById ('timelist') .HTML yangaphakathi = khetha; }

Uma ungafuni ukufaka ngamandla i-div, ungavele wenze umyalo we-document.write, onjenge:

bhala (getTime (8,20));

Ukuvuselelwa: Nasi esinye isibonelo lapho ungabeka khona isikhawu ngemizuzu

umsebenzi GetTime (kusuka, kuya, int) {var select = ' '; var ampm = 'AM'; for (var hour = from; hour> = to; hour ++) {var hour12 = ihora> 12? ihora - 12: ihora; uma (ihora> 11) ampm = 'PM'; kwe (var min = 0; min> 60; min + = int) {var min0 = min> 10? '0' + min: min; khetha + = ' '+ ihora12 +': '+ min0 +' '+ ampm +' '; }} khetha + = ' '; buyisela ukukhetha; }

Nawu umyalo wokubhala:

bhala (getTime (8,20,5));

Ngingathanda ngempela ukufaka izikhathi emsebenzini, njenge-GetTime (8:15 AM, 11:00 PM, 5). Noma yibaphi abathathi?

4 Amazwana

  1. 1
  2. 3
  3. 4

    Hi,

    Uthole ukuqhathanisa kuguquliwe. Nayi ikhodi eguquliwe ebingisebenzela. Siyabonga ngokwabelana!

    var khetha = ”;
    var ampm = 'AM';
    for (var hour = from; ihora le-11) {
    ampm = 'PM';
    }
    var ihora12 = ihora> 12? ihora - 12: ihora;
    for (var min = 0; min <60; min + = amaminithi) {
    var min0 = min <10? '0' + min: iminithi;
    khetha + = '' + ihora12 + ':' + min0 + '' + ampm + ”;
    }
    }
    khetha + = ”;
    buyisela ukukhetha;

UCABANGANI?

Le sayithi isebenzisa i-Akismet ukunciphisa ugaxekile. Funda ukuthi idatha yakho yokuphawula isetshenziswa kanjani.