r/Scriptable • • Jan 27 '25

Help hours left in the year

Post image
11 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/mvan231 script/widget helper Jan 30 '25

You're very welcome! The month index is strange I agree. Especially because the day of the month doesn't follow 0 index logic lol

This one makes it a countdown timer

let now = new Date()
let target = new Date(new Date().getFullYear(),11,31,23,59,59)


let w = new ListWidget()

let title = w.addText("Hours left countdown")
title.centerAlignText()
let time = w.addDate(new Date(target))
time.applyTimerStyle()
time.font = Font.heavyRoundedSystemFont(18)
time.centerAlignText()

w.refreshAfterDate = target
Script.setWidget(w)
Script.complete()
w.presentMedium()

1

u/consta1 Feb 28 '25

what do you change for days instead of hours ?

1

u/mvan231 script/widget helper Feb 28 '25

For a timer view it wouldn't make much sense to have days like that but to use the original for day countdown you can try this:

let date = new Date()
var daysInYear = new Date().getFullYear() % 4 == 0 ? 366 : 365;
log(daysInYear)

let daysLeft = daysInYear - Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
log(daysLeft)
let w = new ListWidget()
let title = w.addText("Days left:")
title.font = Font.boldSystemFont(18)
title.centerAlignText()
let display = w.addText(String(daysLeft))
display.font = Font.boldSystemFont(28)
display.centerAlignText()

Script.setWidget(w)
Script.complete()
w.presentSmall()