aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/js-small-apps/clock/app.js
blob: 7ae2702fff62c4188d3cce65997eeb77b080e01e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function setDate(day, month, year) {
  const div = document.getElementById("date");
  div.textContent = `${day}/${month}/${year}`;
}

function get12Rotation(int) {
  const hour = int > 12 ? int - 12 : int;
  return (360 / 12) * hour;
}

function get60Rotation(int) {
  return (360 / 60) * int;
}

function setSvgClockHours(hours) {
  const clockHours = document.getElementById("svg-clock_hours");
  clockHours.style.transform = `rotate(${get12Rotation(hours)}deg)`;
  clockHours.style.transformOrigin = "center";
}

function setSvgClockMinutes(minutes) {
  const clockMinutes = document.getElementById("svg-clock_minutes");
  clockMinutes.style.transform = `rotate(${get60Rotation(minutes)}deg)`;
  clockMinutes.style.transformOrigin = "center";
}

function setSvgClockSeconds(seconds) {
  const clockSeconds = document.getElementById("svg-clock_seconds");
  clockSeconds.style.transform = `rotate(${get60Rotation(seconds)}deg)`;
  clockSeconds.style.transformOrigin = "center";
}

function setDigitalClockHours(hours) {
  const clockHours = document.getElementById("digital-clock_hours");
  clockHours.textContent = hours;
}

function setDigitalClockMinutes(minutes) {
  const formatted = minutes < 10 ? `0` + minutes : minutes;
  const clockMinutes = document.getElementById("digital-clock_minutes");
  clockMinutes.textContent = formatted;
}

function getHoursToString(hours) {
  const hoursToText = [
    "noon",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
  ];

  return hoursToText[hours % 12];
}

function getMinutesToString(minutes) {
  const ones = [
    "",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
  ];
  const teens = [
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen",
  ];
  const tens = ["", "", "twenty", "thirty", "forty", "fifty"];
  const minutesToArray = minutes.toString().split("");
  let text = "";

  if (minutes < 10) {
    text = ones[minutes];
  } else if (minutes < 20) {
    text = teens[minutesToArray[1]];
  } else {
    text = `${tens[minutesToArray[0]]} ${ones[minutesToArray[1]]}`;
  }

  return text;
}

function setTextClock(hours, minutes) {
  const div = document.getElementById("text-clock");
  const meridiem = hours < 12 ? "am" : "pm";
  div.textContent = `It's ${getHoursToString(hours)} ${getMinutesToString(
    minutes
  )} ${meridiem}.`;
}

function updateAll() {
  const now = new Date();
  const [month, day, year] = [
    now.getMonth() + 1,
    now.getDate(),
    now.getFullYear(),
  ];
  const [hours, minutes, seconds] = [
    now.getHours(),
    now.getMinutes(),
    now.getSeconds(),
  ];

  setDate(day, month, year);
  setSvgClockHours(hours);
  setSvgClockMinutes(minutes);
  setSvgClockSeconds(seconds);
  setDigitalClockHours(hours);
  setDigitalClockMinutes(minutes);
  setTextClock(hours, minutes);
}

updateAll();
setInterval(updateAll, 1000);