JSFiddle - React, Tailwind, and code Playground
by Zachary Shuford
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<clock>
JavaScript
angular.module('app', []);
angular.module('app')
.directive('clock' , function(){
return {
restrict: 'E',
scope: {
format: "@",
date: "@"
},
template: '<div>{{date}}</div>',
link: function(scope, elem, attr){
scope.date = getDate();
setInterval(function(){
scope.date = getDate();
}, 100);
}
}
})
function getDate(){
var now = new Date();
var hour = now.getHours();
var amPm = hour >= 12 ? "PM" : "AM";
if (hour > 12){
hour -= 12;
}
return hour + ":" +
pad(now.getMinutes(), 2) + ":" +
pad(now.getSeconds(), 2) + " " +
amPm;
}
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}