Homey Community Forum

Calculate date of tomorrow

Hi there

I’m tying to create a variable for the date of tomorrow. I’ve taken multiple attempts with logics but I still haven’t succeeded.

My goal is to have a label for the date of tomorrow so I can use this for push notifications . Can anyone help me out with an example how I can arrange this?

Thanks in advance.

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.

image

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 :wink:

2 Likes

Hi Satoer,

Thanks for the advice

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? :slight_smile:

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

Flow1: Saving the day, month and year into variables
Flow2: Checking if the day is the last day of year
Flow3: Checking if the day is the last day of February
Flow4: Checking if the day is the last day of month with 30 days
Flow5: Checking if the day is the last day of month with 31 days or else add only one day

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.  
    return 1+(today - thisYear)/aday;
};
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 :slight_smile:

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 :frowning:

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. :frowning:

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 :slight_smile: 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 :slight_smile:
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;