dkdelta
dkdelta

Reputation: 53

How to get the week number by the model's time in Anylogic?

How can I get the week number of the year by the model's time in Anylogic?

Thank for your help!

Upvotes: 1

Views: 167

Answers (2)

dkdelta
dkdelta

Reputation: 53

This is my code that returns the week number by the model's time (if the week starts on Monday):

int weekNumber = 1;
int dayOfWeekNewYear = 1;
if (getDayOfWeek(toDate(getYear(), 0, 1, 0, 0, 1)) == 1) {
    dayOfWeekNewYear = 7;
}
else {
    dayOfWeekNewYear = getDayOfWeek(toDate(getYear(), 0, 1, 0, 0, 1)) - 1;
}
if (dayOfWeekNewYear <= 4) {
    weekNumber = (int)Math.ceil((double)(getDayOfYear(date()) + (dayOfWeekNewYear - 1))/7);
}
else {
    if ((getDayOfYear(date()) - (8 - dayOfWeekNewYear)) > 0) {
        weekNumber = (int)Math.ceil((double)(getDayOfYear(date()) - (8 - dayOfWeekNewYear))/7);
    }
    else {
        weekNumber = 52;
    }
}

Upvotes: 0

Benjamin
Benjamin

Reputation: 12660

Simply call time(WEEK)

Returns the current model time in weeks.

If you need the relative week number of a year (0-51), you can use getDayOfYear(date()) / 52. and then round it how you like. getDayOfYear gives the relative day of a year for the current model time (0-364)

Upvotes: 1

Related Questions