From f1179ca95cfc315ec04bade27ca2f11842d47e87 Mon Sep 17 00:00:00 2001 From: Villhellm Date: Thu, 13 Aug 2020 09:37:48 -0700 Subject: [PATCH] added 12 hour time option --- README.md | 7 +++++-- dist/clock-card.js | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e876ea6..8f2d242 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,15 @@ A simple analog clock card for Home Assistant. Colors are based on your current ## Display date string format options | String | Returns | ---- | ---- -| hh | current hour with leading zero eg: `04` -| h | current hour eg: `4` +| a | AM/PM +| hh | current 12/hr hour with leading zero eg: `04` +| h | current 12/hr hour eg: `4` | mm | current minute with leading zero eg: `09` | m | current minute eg: `9` | ss | current second with leading zero eg: `07` | s | current second eg: `7` +| HH | current 24/hr hour with leading zero eg: `04` +| H | current 24/hr hour eg: `16` | YYYY | current full year eg: `2020` | YY | current abbreviated year eg: `20` | MMMM | current month name eg: `August` diff --git a/dist/clock-card.js b/dist/clock-card.js index 50bbd13..370896d 100644 --- a/dist/clock-card.js +++ b/dist/clock-card.js @@ -10,12 +10,17 @@ function leadingZero(numberString) { Date.prototype.format = function (formatString) { var dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - formatString = formatString.replace(/hh/g, leadingZero(this.getHours())); - formatString = formatString.replace(/h/g, this.getHours()); + var twlv = this.getHours() == 0 ? 12 : this.getHours() > 12 ? this.getHours() - 12 : this.getHours(); + var ampm = this.getHours() >= 12 ? "PM" : "AM"; + + formatString = formatString.replace(/hh/g, leadingZero(twlv)); + formatString = formatString.replace(/h/g, twlv); formatString = formatString.replace(/mm/g, leadingZero(this.getMinutes())); formatString = formatString.replace(/m/g, this.getMinutes()); formatString = formatString.replace(/ss/g, leadingZero(this.getSeconds())); formatString = formatString.replace(/s/g, this.getSeconds()); + formatString = formatString.replace(/HH/g, leadingZero(this.getHours())); + formatString = formatString.replace(/H/g, this.getHours()); formatString = formatString.replace(/YYYY/g, this.getFullYear()); formatString = formatString.replace(/YY/g, this.getFullYear().toString().substr(2)); formatString = formatString.replace(/MMMM/g, 'ZZZZ'); @@ -27,6 +32,7 @@ Date.prototype.format = function (formatString) { formatString = formatString.replace(/DD/g, leadingZero(this.getDate())); formatString = formatString.replace(/D/g, this.getDate()); + formatString = formatString.replace(/a/g, ampm); formatString = formatString.replace(/ZZZZ/g, monthNames[this.getMonth() + 12]); formatString = formatString.replace(/ZZZ/g, monthNames[this.getMonth()]); formatString = formatString.replace(/XXXX/g, dayNames[this.getDay() + 7]);