Advertisement
Kamend1

02.Time converter

Mar 27th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let convertBtns = Array.from(document.querySelectorAll("input[type='submit']"));
  3.  
  4.     for (let btn of convertBtns) {
  5.         btn.addEventListener('click', (e) => {
  6.             e.preventDefault();
  7.             convertUnits(e);
  8.          });
  9.     }
  10.  
  11.     let days = document.getElementById('days-input');
  12.     let hours = document.getElementById('hours-input');
  13.     let minutes = document.getElementById('minutes-input');
  14.     let seconds = document.getElementById('seconds-input');
  15.  
  16.     function convertUnits (event) {
  17.         let currentEventUnit = event.currentTarget.parentElement.id;
  18.         if(currentEventUnit === 'days') {
  19.             let value = Number(days.value);
  20.             hours.value = (value * 24).toFixed(2);
  21.             minutes.value = (value * 1440).toFixed(2);
  22.             seconds.value = (value * 86400).toFixed(2);
  23.         } else if (currentEventUnit === 'hours') {
  24.             let value = Number(hours.value);
  25.             days.value = (value / 24).toFixed(2);
  26.             minutes.value = (value * 60).toFixed(2);
  27.             seconds.value = (value * 3600).toFixed(2);
  28.         } else if (currentEventUnit === 'minutes') {
  29.             let value = Number(minutes.value);
  30.             days.value = (value / 1440).toFixed(2);
  31.             hours.value = (value / 60).toFixed(2);
  32.             seconds.value = (value * 60).toFixed(2);
  33.         } else if (currentEventUnit === 'seconds') {
  34.             let value = Number(seconds.value);
  35.             days.value = (value / 86400).toFixed(2);
  36.             hours.value = (value / 3600).toFixed(2);
  37.             minutes.value = (value / 60).toFixed(2);
  38.         }
  39.     }
  40.    
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement