How to get the number of week from a date ?

How to get the number of week from a date ?

NicolasMembrez
Not applicable
18 Views
2 Replies
Message 1 of 3

How to get the number of week from a date ?

NicolasMembrez
Not applicable

[ FlexSim 22.2.1 ]

In global table, I have dates. like : "07.01.2019 08:22". I would like to get the number of the week.

In Excel, there is un function "=NO.SEMAINE.ISO()" which give the good answer.

1666795067075.png

Is there a way in Flexsim to get the same result ?

Or another way to get the number of the week ?

Thanks in advance

0 Likes
Accepted solutions (1)
19 Views
2 Replies
Replies (2)
Message 2 of 3

moehlmann_fe
Observer
Observer
Accepted solution

Not directly, as far as I know. There sadly is no direct method to get the week in the Datetime class. You can still use the remaining methods to calculate the week though.

string date = "01.01.2021 08:22";
// Construct datetime from string
DateTime dateTime = DateTime(date, "%d.%m.%Y %H:%M");
int weeks = 0;
int curYear = dateTime.year;
// Find previous monday
while(dateTime.dayOfWeek != 2)
{
    dateTime -= DateTime(86400);
}
// Check if year changed
if(dateTime.year != curYear)
{
    if(dateTime.day > 28)
    {
        // Is first calendar week
        return 1;
    }
    // Is part of last year's last week
    curYear = dateTime.year;
}
// Go to start of year
while(dateTime.year == curYear)
{
    dateTime -= DateTime(604800);
    weeks++;
}
// Check if week belongs to current or last year
if(dateTime.day > 28)
{
    weeks++;
}
return weeks;
0 Likes
Message 3 of 3

NicolasMembrez
Not applicable
Seriously, it works ! thanks a lot !


I think this commande must be proposed to the dev. @Jordan Johnson
0 Likes