Calculate date of tomorrow

I made a few flows which determine the month (January, February etc.) and day (1st day, 2nd day etc.). I needed them to be in separate logic cards for my flows. I found it easier to determine those seperately, rather than determining the exact date in one card.

It basically boils down to twelve different flows for the months (which checks every day at 0:05 in the night which month it is) and a flow that sets a number variable to 1 when it’s the first of the month, and increments it otherwise, every day at 0:04 in the night. That way it doesn’t matter whether the month has 31 or 30 days (or 29/28 > February). All you need to do, is adjust the flow for the day. I’ll share it in a bit.

Hi Neuron 44,

Thanks for your feedback. I will give it a try.

Watch this video before you want to do anything with date calculations

2 Likes

Hi Satoer,

Have u managed it allready? Coz the “solution” of Neuron44 is calculating the day of today, and not of tomorrow.

I still can’t do it.
What i want, is to know if a certain date is tomorrow or the day after tomorrow. Or… i have a date, and i want to know what day of the week that is.

Can’t you use the app icalcalender ?

Iam, butthat can’t do it, yet… more logic would be javascript logic From the same creator. I allready have requested a lot of features. But apparently there are more people that want the same. Why creating a new topic, or solve it behind others back?

I also requested date calculations at better logic but that creator does not respond. So, lets try it this way. Somehow, someway, sombody will make it possible. It is very weird that almost everything is possible but not calculating time/date. I am a theorist, sadly i cannot program however, i do understand how it works. So, i know it is very easy to make it possible for a programmer.

In this case, OT never got an awnser, and he never came back to it, so i am curriuous.

You need five flows

What you want to do with the result is up to you.

Leap year is not taken into account. If you want to take that into account just divide the February checking into two flows.

1 Like

Found an example in HomeyScript that can help you further, I hope, will need some adjustments for you:

``````// getDayOfYear.js
// Get the Day of the Year and set it to a BetterLogic Var
// first create a numeric variable in BetterLogic with the name below: DayOfYear
let BLVarName="DayOfYear";
let testDay = new Date();
Date.prototype.getDayOfYear = function() {
let thisYear = new Date(this.getFullYear(),0,1,0,0,0,0); //yyyy,jan=0, 1st day = 1, 0h,0min.,0 Sec.,0 mSec.
let today = new Date(this.getFullYear(), this.getMonth(), this.getDate(),0,0,0,0); //today , 0h,0min.,0 Sec.,0 mSec.
let aday = 24*60*60*1000; // 24h x Min x Sec x mSec.
};
let dayOfYear = testDay.getDayOfYear();
console.log(dayOfYear);
console.log(Math.round(dayOfYear));
let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
let result = await BLApp.apiPut("/" + BLVarName + "/" + Math.round(dayOfYear) );
return true;
``````

For the Day of week:

``````// my script createDayTag.js
const DAYS = ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'];
var Weekdag = DAYS[new Date().getDay()];
await setTagValue("weekdag", {type: "string", title: "Weekdag"}, DAYS[new Date().getDay()]);
console.log("weekdag", Weekdag);
return(true);``````
1 Like

Two awesome replies… however, i dont understand (yet) what it all means but i will figure that out.
I can read js so i bet i can rewrite some… i know about homeyscript but coz i am not a coder, i never took time for it… i will give it a 8th chance

Thnx!

I fixed it!!
The output is today: 19-09-2020
the script is now:

let BLVarName=“DatumVanMorgen”;
let CurrentDate = new Date();

TomorrowDate = ‘d-m-Y’
.replace(‘d’, (“0” + (CurrentDate.getDate() + 1)).toString().slice(-2))
.replace(‘m’, (“0” + (CurrentDate.getMonth() + 1)).toString().slice(-2))
.replace(‘Y’, CurrentDate.getFullYear());

console.log(TomorrowDate);
let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });
let result = await BLApp.apiPut("/" + BLVarName + “/” + TomorrowDate);
return true;

1 Like

Perhaps test this a bit better, because it will fail on most last-day-of-the-month days

For example:

• the next day from 31-1-2020 will be 32-1-2020
• the next day from 29-2-2020 will be 30-2-2020
• the next day from 30-4-2020 will be 31-4-2020

I think this will help. Google is my friend🤗

1 Like

This won’t work coz it’s output is: 2020-09-20T07:02:55.108Z

And I need: 20-09-2020

It is what I thought also. But it will get date by that instance so… but I’ll test it to be sure

You can of course modify your script so it uses that code:

``````let CurrentDate = new Date();

CurrentDate.setDate(CurrentDate.getDate() + 1);

const TomorrowDate = 'd-m-Y'
.replace('d', ('0' + CurrentDate.getDate()).toString().slice(-2))
.replace('m', ('0' + CurrentDate.getMonth() + 1).toString().slice(-2))
.replace('Y', CurrentDate.getFullYear());
``````

just test it, but both, mine and that line will make it 31 sept.

holy moly… if i set date to 30 sept, it will be 31, when i set 31 sept, it will be 1 okt. :S how weird?

i think i allready know why… but as far as i know now, i cannot test en should wait till 30 sept.
however, i think als now that the add of Marcel_Ubels is good suggestion so that i added now.

Install Node.js (or open the developer console of every recent-ish browser) and you can test all the date-related code easily from the commandline.

Also, my example code contains an issue, you need to add parentheses around the `getMonth()` code:

``````let CurrentDate = new Date();

CurrentDate.setDate(CurrentDate.getDate() + 1);

const TomorrowDate = 'd-m-Y'
.replace('d', ('0' + CurrentDate.getDate()).toString().slice(-2))
.replace('m', ('0' + (CurrentDate.getMonth() + 1)).toString().slice(-2))
.replace('Y', CurrentDate.getFullYear());
``````

i found my own fault, i set setdate with month 9, of course it had to be 8
with your correction, i have tested it, and now it works!!!

thank you very much!!
my whole script is now:

let BLVarName=“DatumVanMorgen”; //the variable for BetterLogic
let CurrentDate = new Date();
//CurrentDate = new Date(2020,8,30)
CurrentDate.setDate(CurrentDate.getDate() + 1);

const TomorrowDate = ‘d-m-Y’
.replace(‘d’, (‘0’ + CurrentDate.getDate()).toString().slice(-2))
.replace(‘m’, (‘0’ + (CurrentDate.getMonth() + 1)).toString().slice(-2))
.replace(‘Y’, CurrentDate.getFullYear());

console.log(TomorrowDate);
let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });
let result = await BLApp.apiPut("/" + BLVarName + “/” + TomorrowDate);
return true;

By The Way… what i have made now, is a fully automated alarmclock.
i add my schedule to apple agenda and with icalCalendar i can now know if i have to work tomorrow or not. before i go to sleep i will get an daily update via google hub. it tells me the temperature, how much power i have collected today, if i have to work or not, etc…
then, the next morning, my alarm will go off 1 hour before i have to start to work. only thing i have to do is adding the schedule.

this was needed coz icalcalender only checks the next event and collect its date. now i can verify if that date is tomorrow or not. as long as that date is not the same, i don’t have to work the next day
At the time 4.30 i run that homeyscript to determine the next day date.

1 Like