question

Javier PL avatar image
0 Likes"
Javier PL asked Javier PL edited

Function that returns the number of days of the current month?

Hello there,

I would like to know if there is a way (method) to get the total number of days of the current month. For example, a function that returns 29 if I passed it the current date (today is 24/02/2020).

Just in case some help may show up, I aimed to design a user command which is able to return the number of days off a resource (which in turn has been assigned to a particular schedule) has taken during a defined period of time. As for now, I'm just considering the normal work schedule (weekends off) and calculating the days off between two passed dates. This way I can get the effective time a sequence task has taken to a specific transport to complete some tasks (much easier if using PF with WaitForEvents and Delays, but I'm not considering this possibility right now). The eventual purpose is to subtract this task time from the next processing time, in order not to influence a fixed whole time of both activities altogether. In other words, it's just a matter of transport visualization, which must not have any influence on activities delays.

Hoping I've been clear enough,

Javi

FlexSim 19.0.2
timescheduledown timemonth
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
2 Likes"
Phil BoBo answered Javier PL edited

You could compose a DateTime that is the first day of the next month, then subtract a second (to get the last day of the current month) and get the day:

DateTime currentDate = DateTime.compose(2020, 2, 24);
int numDaysInMonth = DateTime(DateTime.compose(currentDate.year, (currentDate.month % 12) + 1).as(double) - 1.0).day;
return numDaysInMonth;

Here's the same code with each operation on a separate line instead of all on one line:

DateTime currentDate = DateTime.compose(2020, 2, 24);
DateTime nextMonth = DateTime.compose(currentDate.year, (currentDate.month % 12) + 1);
DateTime lastDayOfCurrentMonth = nextMonth - 1.0; // one second earlier
int numDaysInMonth = lastDayOfCurrentMonth.day;
return numDaysInMonth;

You could put this code into a user command to make it easier to read and reuse.

daysinmonth.fsm

DateTime currentDate = DateTime.compose(2020, 2, 24);
return DaysInMonth(currentDate);

daysinmonth.fsm (23.2 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Javier PL avatar image Javier PL commented ·

Hi @phil.bobo,

Thanks, this seems to be a valid approach attending to what I need. So far I had taken a shortcut by assuming the rule of the knuckles, and calculating the remainder of the division currentMonth%12. However, as a paradigm of programming, I always try to figure out a way that could work in any case (I mean, It's not a matter of being fussy).

However, It's a pity that it does not work in version 19.0.2, cause the method DateTime.compose starts as of FlexSim 20.0.0. as far as I have checked. Thus, I also have developed my own way following your idea, without the use of the method compose. It's a little bit more tricky, but If I'm not wrong, It should work (it's made for the current date in the FlexSim model)

DateTime previousDate = DateTime.days(Model.dateTime.totalDays - Model.dateTime.day+ 33);
DateTime date = DateTime.days(previousDate.totalDays - previousDate.day);
int monthDays = date.day;
return monthDays;<br>

Note: 33 is just a random number >32 in order to completely exceed and get the next month.

Thanks for the contribution!

Javi

1 Like 1 ·
Marcello Rosadini avatar image
2 Likes"
Marcello Rosadini answered Javier PL commented

Hi Javi, I am not aware of such a method (probably someone will propose a more elegant solution).

However, you could have a simple 12 row table manually inputing the number of days for each month in the year you are simulating and then use the month property to look up on that?

something like: doublecurrentMonth=Model.dateTime.month

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Javier PL avatar image Javier PL commented ·

Hi @marcello.rosadini

Thanks for the approach. However, I was looking for a more general way to obtain it, without depending on some user input but on the model's date, which could turn out to be whatever. @phil.bobo's approach seems to be what I was looking for.


Javi

1 Like 1 ·