Skip to content

Instantly share code, notes, and snippets.

@jmscsns

jmscsns/DaysLeft.js

Created Sep 30, 2020
Embed
What would you like to do?
A short script to create a ‘days left’ widget. It’s really just a rip-off of the sample news widget included with Scriptable. The Cabin Sketch font is from Google Fonts and installed with Fontcase.
// Variables
let daysNumber = await loadTimeLeft()
let widget = await createWidget(daysNumber)
// Check if the script is running in
// a widget. If not, show a preview of
// the widget to easier debug it.
if (!config.runsInWidget) {
await widget.presentSmall()
}
// Tell the system to show the widget.
Script.setWidget(widget)
Script.complete()
async function createWidget(items) {
let w = new ListWidget()
w.backgroundColor = new Color("#b00a0f")
let titleTxt = w.addText(daysNumber.toLocaleString())
titleTxt.font = new Font('Cabin Sketch', 96)
titleTxt.centerAlignText()
switch (daysNumber) {
case 1:
var daysText = 'day left.';
break;
default:
var daysText = 'days left.';
}
let subtext = w.addText(daysText)
subtext.font = new Font('Cabin Sketch', 24)
subtext.centerAlignText()
titleTxt.textColor = Color.white()
return w
}
async function loadTimeLeft() {
var end = new Date('12/21/2020 5:00 PM');
var now = new Date();
var left = Math.ceil((end - now)/(1000*60*60*24));
return left;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.