मान लीजिए, हम वर्तमान तारीख और समय का मिलान व्यावसायिक घंटों से कर रहे हैं। हमें वर्तमान समय के आधार पर प्रदर्शित करना है कि ऑफिस अभी बंद है या खुला।
वर्तमान तारीख से घंटे प्राप्त करें और क्लोज और ओपन के लिए टर्नरी ऑपरेटर का उपयोग कर सकते हैं। कोड निम्न है:
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <p class="closeOrOpened"></p> <script> const gettingHours = new Date().getHours() const actualHours = (gettingHours >= 10 && gettingHours < 18) ? 'Open' : 'Closed'; document.querySelector('.closeOrOpened').innerHTML = actualHours; </script> </body> </html>
उपरोक्त प्रोग्राम चलाने के लिए, फाइल का नाम "anyName.html(index.html)" सेव करें और फाइल पर राइट-क्लिक करें। VS कोड एडिटर में "Open with Live Server" विकल्प चुनें।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
Open
अब मेरा वर्तमान समय 10 से कम है, इसलिए मैंने शर्त दी है कि जब घंटा 10 से कम हो तो आउटपुट क्लोज्ड मिलेगा।
