Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Create an Analog Clock using HTML, CSS and JavaScript
Next article icon

Create a Build A Simple Alarm Clock in HTML CSS & JavaScript

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will develop an interactive simple Alarm Clock application using HTML, CSS, and JavaScript languages.

Develop an alarm application featuring date and time input fields for setting alarms. Implement comprehensive validation to prevent duplicate alarms and enforce a maximum limit of three alarms per user. Ensure alarms can only be set for future dates and times, triggering pop-up notifications when they occur.

Preview of final output: Let us have a look at how the final output will look like.

Screenshot-2023-10-04-at-14-44-25-Advanced-Alarm-Clock-min

Prerequisites:

  • HTML
  • CSS
  • JavaScript

Approach:

  • Create the HTML layout structure using the different tags. We have used the h1 and h3 tags to show the GeeksforGeeks title and name of the application. Then we used the <div> class to display the ongoing time to the user. There is <input> of type date to display the date and <input> type time to take the time as an input. There is <button> to trigger the alarm set action.
  • In this styling file, all the styling properties and colors are been specified. Entire colors, padding, and effects are been managed through this file.
  • In this JavaScript code, we first store the reference of HTML elements like 'time', 'alarmDate' etc.
  • Then we have a user-defined function as timeChangeFunction() which is used to show the current running time on screen to the user.
  • There is alarmSetFunction() which is used to set the alarm for the time the user has been entered as an input. Also, the validation is also done here at like same time and cannot have 2 alarms, more than 3 alarms are not allowed.
  • In the showAlarmFunction() the alarms that are set by the user are been shown here, the user can also delete the alarm as per the need.

Steps to Create the Application:

Step 1: Open the VSCode or any other IDE as per your interest.

Step 2: After opening IDE on your local machine create three files index.html, styles.css, and script.js.

Step 3: Write the code of JavaScript in your script file, CSS code in your styles file, and HTML code in the index file.

Example: This example describes the basic implementation of the Simple Alarm Clock application using HTML, CSS, and Javascript.

HTML
<!DOCTYPE html> <html lang="en">  <head> 	<meta charset="UTF-8"> 	<meta name="viewport" 		content="width=device-width,  				initial-scale=1.0"> 	<link rel="stylesheet" href="style.css"> 	<title>Advanced Alarm Clock</title> </head>  <body> 	<div class="clock"> 		<h1> 			GeeksforGeeks 		</h1> 		<h3> 			A Simple Alarm Clock in 			HTML CSS & JavaScript 		</h3> 		<div class="time" id="time"> 			00:00:00 		</div> 		<div class="input-row"> 			<div class="input-field"> 				<label for="alarmDate"> 					Select Date: 				</label> 				<input type="date" 					id="alarmDate" 					class="alarm-input" 					min=""> 			</div> 			<div class="input-field"> 				<label for="alarmTime"> 					Select Time: 				</label> 				<input type="time" id="alarmTime" 					class="alarm-input"> 			</div> 			<button id="setAlarm"> 				Set Alarm 			</button> 		</div> 		<div class="alarms" id="alarms"> 		</div> 	</div> 	<script src="script.js"></script> </body>  </html> 
CSS
body { 	font-family: Arial, sans-serif; 	background: 		linear-gradient(45deg, #007bff, #27ae60); 	color: #fff; 	display: flex; 	justify-content: center; 	align-items: center; 	min-height: 100vh; 	margin: 0; }  .clock { 	text-align: center; 	background-color: #fff; 	border-radius: 10px; 	box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 	padding: 20px; 	width: 300px; }  h1 { 	font-size: 2rem; 	color: green; }  h3 { 	font-size: 1.0rem; 	color: #333; }  .time { 	font-size: 2rem; 	margin: 20px 0; 	color: #333; }  .input-row { 	display: flex; 	flex-direction: column; 	gap: 10px; 	align-items: center; }  .input-field { 	display: flex; 	flex-direction: column; 	align-items: center; }  input[type="date"], input[type="time"] { 	padding: 10px; 	font-size: 1rem; 	border: 1px solid #ccc; 	border-radius: 5px; 	width: 100%; 	max-width: 200px; }  .button-row { 	display: flex; 	gap: 10px; 	justify-content: center; 	align-items: center; }  #setAlarm, #updateTime { 	background-color: #007bff; 	color: #fff; 	border: none; 	padding: 10px 20px; 	border-radius: 5px; 	cursor: pointer; 	font-size: 1rem; 	transition: background-color 0.3s; }  #setAlarm:hover, #updateTime:hover { 	background-color: #0056b3; }  .alarms { 	margin-top: 20px; 	text-align: left; 	color: #333; }  .alarm { 	background-color: #f0f0f0; 	border: 1px solid #ccc; 	border-radius: 5px; 	padding: 10px; 	margin: 10px 0; 	display: flex; 	justify-content: space-between; 	align-items: center; 	animation: fadeIn 0.5s ease-in-out; 	font-size: 14px; }  @keyframes fadeIn { 	from { 		opacity: 0; 	}  	to { 		opacity: 1; 	} } 
JavaScript
let time = document.getElementById("time"); let dateInput = document.getElementById("alarmDate"); let tInput = document.getElementById("alarmTime"); let btn = document.getElementById("setAlarm"); let contan = document.getElementById("alarms"); let interVal; let maxValue = 3; let cnt = 0; let almTimesArray = []; function timeChangeFunction() { 	let curr = new Date(); 	let hrs = curr.getHours(); 	let min = String(curr.getMinutes()).padStart(2, "0"); 	let sec = String(curr.getSeconds()).padStart(2, "0"); 	let period = "AM"; 	if (hrs >= 12) { 		period = "PM"; 		if (hrs > 12) { 			hrs -= 12; 		} 	} 	hrs = String(hrs).padStart(2, "0"); 	time.textContent = `${hrs}:${min}:${sec} ${period}`; } function alarmSetFunction() { 	let now = new Date(); 	let selectedDate = new Date(dateInput.value + "T" + tInput.value); 	if (selectedDate <= now) { 		alert(`Invalid time. Please select  	a future date and time.`); 		return; 	} 	if (almTimesArray.includes(selectedDate.toString())) { 		alert(`You cannot set multiple  	alarms for the same time.`); 		return; 	} 	if (cnt < maxValue) { 		let timeUntilAlarm = selectedDate - now; 		let alarmDiv = document.createElement("div"); 		alarmDiv.classList.add("alarm"); 		alarmDiv.innerHTML = ` 			<span> 			${selectedDate.toLocaleString()} 			</span> 			<button class="delete-alarm"> 			Delete 			</button> 		`; 		alarmDiv 			.querySelector(".delete-alarm") 			.addEventListener("click", () => { 				alarmDiv.remove(); 				cnt--; 				clearTimeout(interVal); 				const idx = almTimesArray.indexOf(selectedDate.toString()); 				if (idx !== -1) { 					almTimesArray.splice(idx, 1); 				} 			}); 		interVal = setTimeout(() => { 			alert("Time to wake up!"); 			alarmDiv.remove(); 			cnt--; 			const alarmIndex = almTimesArray.indexOf(selectedDate.toString()); 			if (alarmIndex !== -1) { 				almTimesArray.splice(alarmIndex, 1); 			} 		}, timeUntilAlarm); 		contan.appendChild(alarmDiv); 		cnt++; 		almTimesArray.push(selectedDate.toString()); 	} else { 		alert("You can only set a maximum of 3 alarms."); 	} } function showAlarmFunction() { 	let alarms = contan.querySelectorAll(".alarm"); 	alarms.forEach((alarm) => { 		let deleteButton = alarm.querySelector(".delete-alarm"); 		deleteButton.addEventListener("click", () => { 			alarmDiv.remove(); 			cnt--; 			clearTimeout(interVal); 			const alarmIndex = almTimesArray.indexOf(selectedDate.toString()); 			if (alarmIndex !== -1) { 				almTimesArray.splice(alarmIndex, 1); 			} 		}); 	}); } showAlarmFunction(); setInterval(timeChangeFunction, 1000); btn.addEventListener("click", alarmSetFunction); timeChangeFunction(); 

Step s to run the Application: Open the live server and search the local host URL in your browser

http://localhost:5500/

Output:


Next Article
Create an Analog Clock using HTML, CSS and JavaScript

G

gauravgandal
Improve
Article Tags :
  • Web Technologies
  • Geeks Premier League
  • JavaScript-Projects
  • Geeks Premier League 2023

Similar Reads

  • Create an Analog Clock using HTML, CSS and JavaScript
    Designing an analog clock is an excellent project to enhance your web development skills. This tutorial will guide you through creating a functional analog clock that displays the current time using HTML, CSS, and JavaScript. What We’re Going to CreateWe will develop a simple analog clock that shows
    5 min read
  • Create a Splash Page App in HTML CSS & JavaScript
    The Splash Screen is the UI element that appears as the initial screen or the loading screen. Splash Page Application consists of a styled animative splash screen effect which automatically makes the curtain opening splash effect into the application for a specific time and renders the dynamic conte
    4 min read
  • Create A Download Button with Timer in HTML CSS and JavaScript
    In this article, we will discuss how to create a Download button with a timer attached to it using HTML, CSS, and Javascript. Our goal is to create a button that has a timer attached to it. The download should only start after the timer has run out, which we will achieve using the setTimeout and set
    2 min read
  • Build A Weather App in HTML CSS & JavaScript
    A weather app contains a user input field for the user, which takes the input of the city name. Once the user enters the city name and clicks on the button, then the API Request is been sent to the OpenWeatherMap and the response is been retrieved in the application which consists of weather, wind s
    7 min read
  • How to create Alarm Setter Card in Tailwind CSS and JavaScript ?
    The Alarm Setter app is a web application built with HTML, Tailwind CSS, and JavaScript. It allows users to input and set an alarm time, receiving timely notifications upon match. Through its design and functionality, users enjoy a seamless experience managing alarms. The application also includes a
    3 min read
  • Create a Coin Flip using HTML, CSS & JavaScript
    We will display the styled INR coin to the user and there will be a styled button (Toss Coin). We will create the entire application structure using HTML and style the application with CSS classes and properties. Here, JavaScript will be used to manage the behavior of the Coin Flip and will be used
    4 min read
  • Build an AI Image Generator Website in HTML CSS and JavaScript
    Create an AI image generator website using HTML, CSS, and JavaScript by developing a user interface that lets users input text prompts and generate images by AI. We incorporated API integration to fetch data, providing users with an effortless and dynamic experience in generating AI-driven images. A
    4 min read
  • Create a Single Page Application using HTML CSS & JavaScript
    In this article, we are going to design and build a cool and user-friendly single-page application (SPA) using just HTML, CSS, and JavaScript. A single-page application contains multiple pages which can be navigated or visited without loading the page every time. This makes things faster and more in
    4 min read
  • How to create A Dynamic Calendar in HTML CSS & JavaScript?
    In this article, we will see how we can create A Dynamic Calendar with the help of HTML CSS & JavaScript. We will be designing and implementing a straightforward yet effective dynamic calendar, offering seamless month navigation and quick access to the current month. With a visually appealing de
    10 min read
  • Design a Simple Counter Using HTML CSS and JavaScript
    Creating a counter application with click tracking is a great beginner-friendly project to enhance your understanding of JavaScript, HTML, and CSS. In this article, we will guide you through building a basic counter app where you can increment or decrement a value, track the number of clicks, and re
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences