BangleApps/apps/daysl/widget.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-03-31 21:17:46 +00:00
const storage = require('Storage');
let settings;
2020-04-04 15:13:39 +00:00
//write settings to file
2020-03-31 21:17:46 +00:00
function updateSettings() {
storage.write('daysleft.json', settings);
2020-04-04 15:13:39 +00:00
}
2020-04-04 15:13:39 +00:00
//Define standard settings
2020-04-04 14:02:34 +00:00
function resetSettings() {
settings = {
day : 17,
month : 6,
year: 2020
};
2020-04-04 15:13:39 +00:00
updateSettings();
2020-04-04 14:02:34 +00:00
}
2020-03-31 21:17:46 +00:00
2020-04-04 15:13:39 +00:00
settings = storage.readJSON('daysleft.json',1); //read storage
if (!settings) resetSettings(); //if settings file was not found, set to standard
var dd = settings.day,
mm = settings.month-1, //-1 because month is zero-based
yy = settings.year;
2020-03-31 21:17:46 +00:00
2020-04-04 14:02:34 +00:00
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
2020-04-04 15:13:39 +00:00
const targetDate = new Date(yy, mm, dd); //is 00:00
const today = new Date(); //includes current time
2020-04-01 19:27:51 +00:00
2020-04-04 14:02:34 +00:00
const currentYear = today.getFullYear();
const currentMonth = today.getMonth();
const currentDay = today.getDate();
2020-04-04 15:13:39 +00:00
const todayMorning = new Date (currentYear, currentMonth, currentDay, 0, 0, 0); //create date object with today, but 00:00:00
2020-04-01 19:27:51 +00:00
2020-04-04 15:13:39 +00:00
const diffDays = (targetDate - todayMorning) / oneDay; //calculate day difference
2020-03-31 21:17:46 +00:00
2020-04-04 14:02:34 +00:00
//draw widget
2020-03-31 21:17:46 +00:00
WIDGETS["daysl"]={area:"tl",width:40,draw:function(){
g.setFont("6x8", 1);
g.drawString(diffDays,this.x+12,this.y+12);
}};