{"id":9864,"date":"2024-01-12T18:09:00","date_gmt":"2024-01-12T18:09:00","guid":{"rendered":"https:\/\/codehim.com\/?p=9864"},"modified":"2024-01-22T16:12:46","modified_gmt":"2024-01-22T11:12:46","slug":"javascript-digital-clock-with-12-hour-format","status":"publish","type":"post","link":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/","title":{"rendered":"JavaScript Digital Clock With 12 Hour Format"},"content":{"rendered":"<p>Looking for a JavaScript digital clock in 12-hour format? This code, written in pure vanilla JS, creates a sleek and stylish timepiece for your webpage. The script initializes variables for hours, minutes, seconds, and AM\/PM, <a href=\"https:\/\/codehim.com\/date-time\/javascript-analog-clock-widget\/\" target=\"_blank\" rel=\"noopener\">displaying the current time<\/a> in a user-friendly format.<\/p>\n<p>The clock updates every second, ensuring real-time accuracy. Easily customizable, it&#8217;s perfect for adding a touch of functionality and elegance to your site.<\/p>\n<h2>How to Create JavaScript Digital Clock With 12 Hour Format<\/h2>\n<p>1. In your HTML file, create a container where you want the clock to appear. For example:<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;div class='time'&gt;&lt;\/div&gt;\r\n&lt;h1&gt;This is what you are looking for.&lt;\/h1&gt;\r\n&lt;h1&gt;Pure Vanilla JS&lt;\/h1&gt;<\/pre>\n<p>Feel free to customize the styling by adding additional HTML elements and classes.<\/p>\n<p>2. Apply the desired styles to your clock using the following CSS code. This example provides a simple dark theme, but you can customize it to match your website&#8217;s design:<\/p>\n<pre class=\"prettyprint linenums lang-css\">body {\r\n  background: black;\r\n}\r\n.time, h1 {\r\n  font-family: Helvetica;\r\n  font-size: 6em;\r\n  color: white;\r\n}<\/pre>\n<p>3. Now, add the JavaScript code to your file. Copy and paste the following script, which initializes variables for hours, minutes, seconds, and AM\/PM. The <code>displayTime<\/code> function formats the time and continuously updates the clock every second.<\/p>\n<pre class=\"prettyprint linenums lang-js\">\/\/ A simple vanilla JS clock\r\n\r\n\/\/ Initilize variables for date, hour, minute, second, am or pm, and the final displayed time\r\nvar d, h, m, s, ampm, time;\r\n\r\n\/\/ Your clock!\r\nfunction displayTime() {\r\n  d = new Date();\r\n  h = d.getHours();\r\n  m = d.getMinutes();\r\n  s = d.getSeconds();\r\n  ampm = h &gt;= 12 ? 'pm' : 'am';\r\n  h = h % 12;\r\n\r\n  \/\/ Adds zeros to single digit times\r\n  if (h &lt;= 9) {\r\n    h = '0' + h;\r\n  }\r\n  if (m &lt;= 9) {\r\n    m = '0' + m;\r\n  }\r\n  if (s &lt;= 9) {\r\n    s = '0' + s;\r\n  }\r\n\r\n  \/\/ Assign time format to variable. If you want to change how time is displayed do it here\r\n  \/\/ Example time = h + \":\" + m;\r\n  time = h + \":\" + m + \":\" + s + \" \" + ampm;\r\n\r\n  \/\/ Print your clock to an element.\r\n  document.getElementsByClassName(\"time\")[0].innerHTML = time;\r\n\r\n  \/\/ Refreshes clock every second. If you're just using minutes change to 60000\r\n  setTimeout(displayTime, 1000);\r\n}\r\n\r\n\/\/ Run your baller clock!\r\ndisplayTime();<\/pre>\n<p>That&#8217;s all! hopefully, you&#8217;ve successfully implemented a digital clock with a 12-hour format on your webpage. If you have any questions or suggestions, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for a JavaScript digital clock in 12-hour format? This code, written in pure vanilla JS, creates a sleek and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":9874,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[58],"tags":[88],"class_list":["post-9864","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-date-time","tag-digital-clocks"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-12T18:09:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T11:12:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"JavaScript Digital Clock With 12 Hour Format\",\"datePublished\":\"2024-01-12T18:09:00+00:00\",\"dateModified\":\"2024-01-22T11:12:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\"},\"wordCount\":208,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png\",\"keywords\":[\"Digital Clocks\"],\"articleSection\":[\"Date &amp; Time\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\",\"url\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\",\"name\":\"JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png\",\"datePublished\":\"2024-01-12T18:09:00+00:00\",\"dateModified\":\"2024-01-22T11:12:46+00:00\",\"description\":\"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png\",\"width\":1280,\"height\":960,\"caption\":\"JavaScript Digital Clock With 12 Hour Format\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Date &amp; Time\",\"item\":\"https:\/\/codehim.com\/category\/date-time\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Digital Clock With 12 Hour Format\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim","description":"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim","og_description":"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.","og_url":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-01-12T18:09:00+00:00","article_modified_time":"2024-01-22T11:12:46+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"JavaScript Digital Clock With 12 Hour Format","datePublished":"2024-01-12T18:09:00+00:00","dateModified":"2024-01-22T11:12:46+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/"},"wordCount":208,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png","keywords":["Digital Clocks"],"articleSection":["Date &amp; Time"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/","url":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/","name":"JavaScript Digital Clock With 12 Hour Format &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png","datePublished":"2024-01-12T18:09:00+00:00","dateModified":"2024-01-22T11:12:46+00:00","description":"Here is a free code snippet to create a JavaScript Digital Clock With 12 Hour Format. You can view demo and download the source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/JavaScript-Digital-Clock-With-12-Hour-Format.png","width":1280,"height":960,"caption":"JavaScript Digital Clock With 12 Hour Format"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/date-time\/javascript-digital-clock-with-12-hour-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Date &amp; Time","item":"https:\/\/codehim.com\/category\/date-time\/"},{"@type":"ListItem","position":3,"name":"JavaScript Digital Clock With 12 Hour Format"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":731,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=9864"}],"version-history":[{"count":0,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/9874"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=9864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=9864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=9864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}