Created
September 18, 2020 22:36
-
-
Save lilyball/0a04158e6b810f25ccf8d64322d01f91 to your computer and use it in GitHub Desktop.
Scriptable script that powers a home screen widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: deep-green; icon-glyph: calendar-alt; | |
let widget = createWidget() | |
if (!config.runsInWidget) { | |
await widget.presentMedium() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
function createWidget() { | |
let fontSize = 36 | |
switch (config.widgetFamily) { | |
case "small": fontSize = 28; break; | |
case "medium": fontSize = 36; break; | |
case "large": fontSize = 48; break; | |
} | |
const widget = new ListWidget() | |
let text = widget.addText("Today is") | |
text.font = Font.mediumSystemFont(fontSize) | |
text = widget.addText("March " + getToday()) | |
text.font = Font.boldSystemFont(fontSize) | |
return widget | |
} | |
// Cribbed from https://github.com/mdayaram/calendar2020 | |
function isLeapYear(date) { | |
const year = date.getFullYear(); | |
if ((year & 3) != 0) return false; | |
return ((year % 100) != 0 || (year % 400) == 0); | |
}; | |
// Get Day of Year | |
function getDOY(date) { | |
const dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; | |
const mn = date.getMonth(); | |
const dn = date.getDate(); | |
let dayOfYear = dayCount[mn] + dn; | |
if (mn > 1 && isLeapYear(date)) dayOfYear++; | |
return dayOfYear; | |
}; | |
function dayOfMarch(dayOfYear = null) { | |
if (dayOfYear === null) { | |
const now = new Date(); | |
dayOfYear = getDOY(now); | |
} | |
return dayOfYear - 31 - 29; // minus January and February. | |
} | |
function getToday() { | |
const day = dayOfMarch(); | |
const ones = day % 10; | |
const tens = (day % 100) - ones; | |
let suffix; | |
if (tens !== 10 && ones === 1) { | |
suffix = "st"; | |
} else if (tens !== 10 && ones === 2) { | |
suffix = "nd"; | |
} else if (tens !== 10 && ones === 3) { | |
suffix = "rd"; | |
} else { | |
suffix = "th"; | |
} | |
return day.toString() + suffix | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment