# Days in current month

**URL:** <https://community.homey.app/t/days-in-current-month/75007>\
**Category:** Questions & Help\
**Tags:** advancedflow\
**Created:** [January 13, 2023, 12:04am UTC](https://community.homey.app/t/days-in-current-month/75007 "2023-01-13T00:04:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 12:04am UTC](https://community.homey.app/t/days-in-current-month/75007/1 "2023-01-13T00:04:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![spkesDE](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/spkesde/32/54352_2.png) [@spkesDE](https://community.homey.app/u/spkesDE)\
**Post date:** [January 13, 2023, 12:25am UTC](https://community.homey.app/t/days-in-current-month/75007/2 "2023-01-13T00:25:10Z")

</div>

You can use [homeyscript](https://homey.app/a/com.athom.homeyscript).

Use a ScriptCard with number return.

```auto
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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 12:29am UTC](https://community.homey.app/t/days-in-current-month/75007/3 "2023-01-13T00:29:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spkesDE](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/spkesde/32/54352_2.png) [@spkesDE](https://community.homey.app/u/spkesDE)\
**Post date:** [January 13, 2023, 12:31am UTC](https://community.homey.app/t/days-in-current-month/75007/4 "2023-01-13T00:31:00Z")

</div>

> [@Torbjorn\_Melbye](#):
>
> Will the first one take leap years into account when returning the number of days?

Yes it does.

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 12:37am UTC](https://community.homey.app/t/days-in-current-month/75007/5 "2023-01-13T00:37:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![spkesDE](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/spkesde/32/54352_2.png) [@spkesDE](https://community.homey.app/u/spkesDE)\
**Post date:** [January 13, 2023, 10:10am UTC](https://community.homey.app/t/days-in-current-month/75007/6 "2023-01-13T10:10:57Z")

</div>

> [@Torbjorn\_Melbye](#):
>
> Just tested it. It returned 30. January has 31 days.

Opps. change that -1 to +1.

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

```

> [@Torbjorn\_Melbye](#):
>
> Also. When the script is run on homey. Where does it “put” the output/number? How do I get it into a variable?

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

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 6:34pm UTC](https://community.homey.app/t/days-in-current-month/75007/7 "2023-01-13T18:34:32Z")

</div>

Perfect, thanks for your help

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 7:26pm UTC](https://community.homey.app/t/days-in-current-month/75007/8 "2023-01-13T19:26:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [January 13, 2023, 7:44pm UTC](https://community.homey.app/t/days-in-current-month/75007/9 "2023-01-13T19:44:05Z")

</div>

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

```auto
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());

```

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 7:52pm UTC](https://community.homey.app/t/days-in-current-month/75007/10 "2023-01-13T19:52:38Z")

</div>

Great, that is perfect! Thanks alot 😃

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 13, 2023, 11:24pm UTC](https://community.homey.app/t/days-in-current-month/75007/11 "2023-01-13T23:24:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [January 14, 2023, 3:50am UTC](https://community.homey.app/t/days-in-current-month/75007/12 "2023-01-14T03:50:00Z")

</div>

Mmm could it be summer/ wintertime? Here is a [link](https://stackoverflow.com/questions/11887934/how-to-check-if-dst-daylight-saving-time-is-in-effect-and-if-so-the-offset/30280636#30280636) 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.

---

<div class="post-metadata">

**Author:** ![Torbjorn\_Melbye](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torbjorn_melbye/32/18194_2.png) [@Torbjorn\_Melbye](https://community.homey.app/u/Torbjorn_Melbye)\
**Post date:** [January 14, 2023, 6:22am UTC](https://community.homey.app/t/days-in-current-month/75007/13 "2023-01-14T06:22:26Z")

</div>

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 😌

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 14, 2023, 7:36am UTC](https://community.homey.app/t/days-in-current-month/75007/14 "2023-01-14T07:36:10Z")

</div>

> [@Torbjorn\_Melbye](#):
>
> Yet another reason to stop the “madness of manipulating time” twice a year.

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

---

<div class="post-metadata">

**Author:** ![Hamster](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hamster/32/16580_2.png) [@Hamster](https://community.homey.app/u/Hamster)\
**Post date:** [January 22, 2023, 8:12am UTC](https://community.homey.app/t/days-in-current-month/75007/15 "2023-01-22T08:12:18Z")

</div>

> [@robertklep](#):
>
> HomeyScript should just provide a method of returning the _local_ datetime, as opposed to a fairly useless UTC datetime.

But HomeyScript can do that, see this example

> [@Actuele tijd weergeven inclusief secondes](https://community.homey.app/t/actuele-tijd-weergeven-inclusief-secondes/75247/2):
>
> Probeer dit script eens: return getCurrentTime("nl-NL","Europe/Amsterdam"); function getCurrentTime(locale, timezone){ let date = new Date(); return date.toLocaleString(locale,{timeZone: timezone }); //return date.toLocaleTimeString(locale,{timeZone: timezone }); //return date.toLocaleDateString(locale,{timeZone: timezone }); } Ik heb het script nog even aangepast. Hij zal nu “datum+tijd” teruggeven, maar je kan ook alleen de tijd of alleen de datum ter…

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 22, 2023, 8:20am UTC](https://community.homey.app/t/days-in-current-month/75007/16 "2023-01-22T08:20:01Z")

</div>

> [@Hamster](#):
>
> But HomeyScript can do that

[I know](https://github.com/athombv/homey-apps-sdk-issues/issues/169#issuecomment-780700570), but we’re dealing with `Date` objects here, not strings.
