Introduction

In this article, we are going to understand how to display a running clock using HTML 5. In this, we will show the polar clock and the date-time from your system clock in the browser.
Here we will use some JavaScript and some styles along with the HTML code. Just go through the steps to see how to create this application.
Let's see how the ClockHTML5 application can be created. To do so go through the following steps.
Step 1 : Open a HTML editor or Visual Studio.
sd.gif
Open File menu ->select new ->Choose Website then.
0000.jpg
This is where we will create an HTML5 application.
clck.gif
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or in between the <head>--</head> tags.
Here two CSS files are used for design purposes; one is for the section div, named d1.css and the other d2.css for the whole body of the web page.
d1.css scripting
  1. body
  2. {
  3. background: #C1FFC1;
  4. color: #fff;
  5. font: 300 100.1% "Helvetica Neue" , Helvetica, "Arial Unicode MS" , Arial, sans-serif;
  6. }
  7. #holder
  8. {
  9. height: 442px;
  10. left: 42%;
  11. margin: -240px 0 0 -320px;
  12. position: absolute;
  13. top: 57%;
  14. width: 842px;
  15. font-size: x-large;
  16. background-color: #008000;
  17. }
  18. #duplicate {
  19. bottom: 0;
  20. font: 300 .7em "Helvetica Neue", Helvetica, ;
  21. position: absolute;
  22. right: 1em;
  23. text-align: right;
  24. }
  25. #duplicate d {
  26. color: #fff;
  27. }
d2.css scripting
  1. body {
  2. background: #fff;
  3. color: #000;
  4. font: 100.1% "Comic Sans MS", Lucida, Verdana, sans-serif;
  5. }
  6. #holder {
  7. height: 480px;
  8. left: 50%;
  9. margin: 0 0 0 -320px;
  10. position: absolute;
  11. top: 0;
  12. width: 640px;
  13. }
  14. #duplicate {
  15. bottom: 0;
  16. font-size: .7em;
  17. position: absolute;
  18. right: 1em;
  19. text-align: right;
  20. }
Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works download the attached .rar file and run the ClockHTML5 application. The whole JavaScript looks as in the following.
  1. window.onload = function ()
  2. {
  3. var dee = Color("holder", 600, 600),
  4. Rid = 200,
  5. init = true,
  6. param = { stroke: "#fff", "stroke-width": 30 },
  7. hash = document.location.hash,
  8. marksAttr = { fill: hash || "#444", stroke: "none" },
  9. html = [
  10. document.getElementById("h"),
  11. document.getElementById("m"),
  12. document.getElementById("s"),
  13. document.getElementById("d"),
  14. document.getElementById("mnth"),
  15. document.getElementById("ampm")
  16. ];
  17. // Custom Attribute
  18. dee.customAttributes.arc = function (value, total, Rid)
  19. {
  20. var beta = 360 / total * value,
  21. p = (90 - alpha) * Math.PI / 180,
  22. x = 300 + Rid * Math.cos(a),
  23. y = 300 - Rid * Math.sin(a),
  24. color = "hsb(".concat(Math.round(Rid) / 200, ",", value / total, ", .75)"),
  25. way;
  26. if (total == value)
  27. {
  28. way = [["M", 300, 300 - Rid], ["A", Rid, Rid, 0, 1, 1, 299.99, 300 - Rid]];
  29. }
  30. else
  31. {
  32. way = [["M", 300, 300 - Rid], ["A", Rid, Rid, 0, +(alpha > 180), 1, x, y]];
  33. }
  34. return { way: way, stroke: color };
  35. };
  36. function drawMarks(Rid, total)
  37. {
  38. if (total == 31)
  39. { // month
  40. var dt = new Date;
  41. dt.setDate(1);
  42. dt.setMonth(dt.getMonth() + 1);
  43. dt.setDate(-1);
  44. total = dt.getDate();
  45. }
  46. var color = "hsb(".concat(Math.round(Rid) / 200, ", 1, .75)"),
  47. out = dee.set();
  48. for (var value = 0; value < total; value++)
  49. {
  50. var alpha = 360 / total * value,
  51. p = (90 - alpha) * Math.PI / 180,
  52. x = 300 + Rid * Math.cos(a),
  53. y = 300 - Rid * Math.sin(a);
  54. out.push(r.circle(x, y, 2).attr(marksAttr));
  55. }
  56. return out;
  57. }
  58. (function ()
  59. {
  60. var dt = new Date,
  61. am = (dt.getHours() < 12),
  62. h = dt.getHours() % 12 || 12;
  63. updateVal(dt.getSeconds(), 60, 200, sec, 2);
  64. updateVal(dt.getMinutes(), 60, 160, min, 1);
  65. updateVal(h, 12, 120, hor, 0);
  66. updateVal(dt.getDate(), 31, 80, day, 3);
  67. updateVal(dt.getMonth() + 1, 12, 40, mon, 4);
  68. pm[(am ? "hide" : "show")]();
  69. html[5].innerHTML = am ? "AM" : "PM";
  70. setTimeout(arguments.callee, 1000);
  71. init = false;
  72. })();
  73. };
Step 4: In this section, we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the clockHtml5.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.
  1. <body>
  2. <div id="holder"></div>
  3. <div id="time">
  4. <span id="h"></span>:<span id="m"></span>:<span id="s"></span> <span id="ampm"></span> Date <span id="d"></span>/<span id="mnth"></span>
  5. </div>
  6. </body>
Step 5 : The complete code for the ClockHTML5 application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="clockHtml5.aspx.cs" Inherits="Reflectionimage._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta charset="utf-8">
  6. <title></title>
  7. <link rel="stylesheet" href="d1.css" media="screen">
  8. <link rel="stylesheet" href="d2.css" media="print">
  9. <script src="javascript.js"></script>
  10. <script type="text/javascript">
  11. From Step 3 JavaScript copy from there and paste it here.
  12. </script
  13. <style media="screen">
  14. #holder {
  15. height: 600px;
  16. margin: -300px 0 0 -300px;
  17. width: 600px;
  18. }
  19. #time {
  20. text-align: center;
  21. font: 80 3em "Helvetica Neue", Helvetica, Arial, sans-serif;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="holder"></div>
  27. <div id="time">
  28. <span id="h"></span>:<span id="m"></span>:<span id="s"></span> <span id="ampm"></span> Date <span id="d"></span>/<span id="mnth"></span>
  29. </div>
  30. </body>
  31. </html>
Step 6 : Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see here the polar clock and the date and time of your system in the browser.
clck1.gif
Time is running continuously, you can watch in the given figure.
clck2.gif
Here are some useful resources
Display video using HTML5: Part 1
Analog Clock Using Microsoft Expression Blend
Analog Clock Widget in C#
Analog Clock Widget