Skip to content

Commit

Permalink
added 12 hour time option
Browse files Browse the repository at this point in the history
  • Loading branch information
Villhellm committed Aug 13, 2020
1 parent 79f348c commit f1179ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 8 additions & 2 deletions dist/clock-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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]);
Expand Down

0 comments on commit f1179ca

Please sign in to comment.