Days in current month

Trying to create a flow to calculate a number devided by the number of days in the month.
The problem is that I don’t have a variable with the number of days in the current month.
Is there an app, that provides this, or a flow that could generate it?
An advanced flow could almost do it, but leap years with an extra day in February is where I get stuck.

You can use homeyscript.

Use a ScriptCard with number return.

let date = new Date(); //Get Current Date
return new Date(date.getFullYear(), date.getMonth()-1, 0).getDate(); //Return current days of the month

And if u want to know if its a leap year

let date = new Date(); //Get Current date

//Calculate if its a leap year
return date.getFullYear() % 400 === 0 || (date.getFullYear() % 100 !== 0 && date.getFullYear() % 4 === 0) ? 366 : 365
1 Like

Thank you.
I’m not well versed in JavaScript.
Will the first one take leap years into account when returning the number of days?
I don’t really need to know if it’s a leap year, but my calculations will need to take this into consideration

Yes it does.

Ok.
Couple of things.
Just tested it. It returned 30. January has 31 days.
Also. When the script is run on homey. Where does it “put” the output/number? How do I get it into a variable?

Edit: just saw that advanced flow is required for getting the number into a variable. On my phone at the moment, but will try this at home.

Opps. change that -1 to +1.

let date = new Date(); //Get Current Date
return new Date(date.getFullYear(), date.getMonth()+1, 0).getDate(); //Return current days of the month

You can use is as a token in adv. flows or put them into a global variable using the according card for it

Perfect, thanks for your help

Need some further help. I need the day of the month as well.

you could try this script. Copy it in Homeyscript, give the script a name. Make a flow to run this script everyday @ 00.01. This way you get a few tags you can use.
Example tag Yesterday, tag Today, tag DaysInCurrentMonth etc etc

var date = new Date();
date.setDate(date.getDate());
const currentDay = date.getDate();
console.log('9 currentDay is: ' +currentDay);
await tag('CurrentDay', currentDay);
const currentMonth = date.getMonth() +1; // getMonth() returns month from 1 to 12
console.log('10 currentMonth is: '+currentMonth);
await tag('CurrentMonth', currentMonth);
const currentYear = date.getFullYear();
console.log('11 currentYear is: '+currentYear);
await tag('CurrentYear', currentYear);


var date = new Date(); // current date
date.setDate(1); // going to 1st of the month
date.setHours(-1);// going to the last day of the previous Month
const yearPreviousMonth = date.getFullYear();
console.log('12 yearPreviousMonth is: '+yearPreviousMonth);
await tag('YearPreviousMonth', yearPreviousMonth);
const lastDayPreviousMonth = date.getDate();
console.log('13 lastDayPreviousMonth is: '+lastDayPreviousMonth);
await tag('LastDayPreviousMonth', lastDayPreviousMonth);
const previousMonth = date.getMonth() +1;
console.log('14 previousMonth is: '+previousMonth);
await tag('PreviousMonth', previousMonth);


var date = new Date();
date.setDate(date.getDate());
function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();
}
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth);
console.log('15 daysInCurrentMonth is: '+daysInCurrentMonth);
await tag('DaysInCurrentMonth', daysInCurrentMonth);
const daysInPreviousMonth = getDaysInMonth(currentYear, previousMonth);
console.log('16 daysInPreviousMonth '+previousMonth +' is: '+daysInPreviousMonth);
await tag('DaysInPreviousMonth', daysInPreviousMonth);

var date = new Date();
date.setDate(date.getDate()-1);
const yesterDay = date.getDate();
console.log('17 yesterDay is: ' +yesterDay);
await tag('YesterDay', yesterDay);
const yesterDayMonth = date.getMonth() +1; // getMonth() returns month from 0 to 11
console.log('18 yesterDayMonth is: ' +yesterDayMonth);
await tag('YesterDayMonth', yesterDayMonth);
const yesterDayYear = date.getFullYear();
console.log('19 yesterDayYear is: '+yesterDayYear);
await tag('YesterDayYear', yesterDayYear);

var date = new Date();
date.setDate(date.getDate());
1 Like

Great, that is perfect! Thanks alot :smiley:

For some reason, the script does not update now that we have crossed into a new day.
Still gives 13 as day number even though it is now the 14th.
What could be the reason for this?
I’ve checked that homey has the correct time.

Mmm could it be summer/ wintertime? Here is a link to a partly solution. But time is quite trickey. You could make a new topic, to get a better/ more complete solution

If the above is the problem and summertime/ wintertime. Isn’t relevant for you. You could run the script at another moment.

Well, daylight savings is in effect where I live in Norway, so I guess that’s it.
It seems to return correct dates now, so likely it is just off by an hour. Will have to check tonight to be sure. Yet another reason to stop the “madness of manipulating time” twice a year.
I guess I could adjust the flow to run the script an hour later, and live with the fact that the calculations that use the tags are wrong for an hour every day (In the winter at least).

Don’t know enough about JavaScript to understand alot from the link you provided. I might learn though.
It’s weird to me that the code doesn’t fetch local time correctly, when the device it runs on clearly does. But again. Don’t really know how it works.

Thanks for your help :relaxed:

HomeyScript should just provide a method of returning the local datetime, as opposed to a fairly useless UTC datetime.

3 Likes

But HomeyScript can do that, see this example

I know, but we’re dealing with Date objects here, not strings.