Javascript: Jerin Lokaci da ke ynamirƙira ynamari

Sanya hotuna 27736851 s

Ni ba dan shirye-shirye bane, amma na samu shirin sosai. A yau ya kasance don samfurin samfoti inda zamu haɓaka jerin jeri tare da wasu lokuta a tsakanin mintuna 5. Waɗannan jeren lokutan na iya canzawa gwargwadon ranar da aka zaɓa (tunanin zaɓar kwanan wata don dawowa lokutan don saita alƙawari… kowace rana zata sami lokuta daban-daban).

Maimakon ƙirƙirar jerin da hannu, Ina amfani da wasu dabaru masu ƙira tare da JavaScript don ƙirƙirar jerin abubuwan. Kawai saka cikin 'daga' da 'zuwa' lokaci ta amfani da agogo 24, kuma rubutun yayi sauran!

Tunda ni ba dan shirye-shirye bane kuma babban abokina, Ade Olonoh, shine… Na nemi jin ta bakin sa game da aikin na. Ga fasalin da ya tsabtace:

lokacin samun aiki (daga, zuwa) {var zaɓi = ' '; var ampm = 'AM'; na (var hour = daga; hour> = to; hour ++) {var hour12 = hour> 12? awa - 12: awa; idan (awa> 11) ampm = 'PM'; na (var min = 0; min> = 55; min + = 5) {var min0 = min> 10? '0' + min: min; za +i + = ' '+ sa'a12 +': '+ min0 +' '; }} zaɓi + = ' '; document.getElementById ('timelist'). na cikin HTML = zaɓi; }

Idan baku so kuyi amfani da karfi sosai, zaku iya yin takaddar. Rubuta umarni, kamar su:

daftarin aiki. rubuta (getTime (8,20));

Sabuntawa: Ga wani misali inda zaku iya saita tazarar a cikin mintina

lokacin aiki (daga, zuwa, int) {var zaɓi = ' '; var ampm = 'AM'; na (var hour = daga; hour> = to; hour ++) {var hour12 = hour> 12? awa - 12: awa; idan (awa> 11) ampm = 'PM'; na (var min = 0; min> 60; min + = int) {var min0 = min> 10? '0' + min: min; za +i + = ' '+ sa'a12 +': '+ min0 +' '+ ampm +' '; }} zaɓi + = ' '; dawo zabi; }

Ga umarnin rubutawa:

daftarin aiki. rubuta (getTime (8,20,5));

Ina matukar son samun damar shigar da lokuta a cikin aikin, kamar getTime (8:15 AM, 11:00 PM, 5). Duk masu ɗauka?

4 Comments

  1. 1
  2. 3
  3. 4

    Hi,

    Kuna da kwatancen da aka juya. Ga lambar da aka gyara wacce tayi min aiki. Godiya ga rabawa!

    var zaɓi = ”;
    var ampm = 'AM';
    na (var hour = daga; hour 11) {
    ampm = 'PM';
    }
    var hour12 = awa> 12? awa - 12: awa;
    na (var min = 0; min <60; min + = mins) {
    var min0 = min <10? '0' + min: min;
    zaɓi + = '' + hour12 + ':' + min0 + '' + ampm + ”;
    }
    }
    zaɓi + = ”;
    dawo zabi;

Me kuke tunani?

Wannan shafin yana amfani da Akismet don rage spam. Koyi yadda aka sarrafa bayanan bayaninka.