Time Functions
Finding out the current model time
You can get current model (logical) time using the function
time(). There are two notations: the simple one,
time()
returns the current model time as number of model time units simulated since the model start. Another,
time(TIME_UNIT)
returns the number of specified time units simulated so far. For example,
time(MINUTE)
returns the number of simulated minutes (a double value).
Finding out the current date, day of week, hour of day, etc.
The date in AnyLogic is stored in the form of the Java class
Date. Date is composed of the year, month, day of month, hour of the day, minute, second, and millisecond. To find out the current date, you should call the function
date().
A number of functions return particular components of the current date (and all those functions also have the form without a parameter, in which case they return the component of a given, not current, date):

int getYear(Date date)
– returns the year of the current date.

int getMonth(Date date)
– returns the month of the current date: one of the constants
JANUARY, FEBRUARY,
…

int getDayOfMonth(Date date)
– returns the day of the month of the current date: 1, 2, …

int getDayOfWeek(Date date)
– returns the day of the week of the current date: one of the constants
SUNDAY, MONDAY, …

int getHourOfDay(Date date)
– returns the hour of the day of the current date in 24hour format: for 10:20 PM, will return 22.

int getHour(Date date)
– returns the hour of the day of the current date in 12hour format: for 10:20 PM, will return 10.

int getAmPm(Date date)
– returns the constant AM if the current date is before noon, and PM otherwise.

int getMinute(Date date)
– returns the minute within the hour of the current date.

int getSecond(Date date)
– returns the second within the minute of the current date.
Functions to make model independent of time unit settings
Let’s say the time unit in your model is hours. What if you need to schedule something to happen in 2 days? Or how would you define a duration of 5 minutes? Of course, you could write 48 and 5.0/60. But a much better solution is to use the special
functions that return the value of a given time interval with respect to the current time unit settings:

double millisecond()
– returns the value of a onemillisecond time interval.

double second()
– returns the value of a onesecond time interval.

double minute()
– returns the value of a oneminute time interval.

double hour()
– returns the value of a onehour time interval.

double day()
– returns the value of a oneday time interval.

double week()
– returns the value of a oneweek time interval.
For example, if the time unit is hours,
minute()
will return 0.0166, and
week()
will return 168.0. Thus, instead of remembering what the current time unit is and writing 48 or 5./60, you can simply write
2*day()
and
5*minute(). You can also combine different units in one expression:
3 * hour() 20 * minute().
What is probably even more important about these functions is that the expressions using them are completely independent of the time unit settings: the expressions always evaluate to the correct time intervals. Therefore, we recommend always using
multipliers such as
minute(),
hour(),
day(), etc. in the numeric expressions that represent time intervals: this way, you can freely change the time units without changing the model.
Conversion functions

double addToDate(Date date, int timeUnit, double amount)
– Returns the date, which will be after the given amount of given time units from the given
date
e.g.
addToDate( DAY, 1 )
returns the 'tomorrow' date: (
date
1 day )

double dateToTime()
– Converts the given date to model time with respect to the start date, start time and model time unit settings.

double differenceInCalendarUnits(
time unit constant, double time1, double time2 )
– Returns the difference (time2  time1) between two model dates (corresponding to the given model times) in the given time units. Result is the number of date units that should be added to the model time
time1
to obtain
time2. The result may be negative and may have fractional part depending on the given dates.

Date dropTime(Date date)
– Drops timeoftheday information and returns the same date but with the time being set to 00:00:00.000

Date timeToDate(double t)
– Converts the given model time to date with respect to the start date, start time and model time unit settings,
null
if the time is infinity.

Date toDate(int year,
int
month,
int
day,
int
hourOfDay,
int
minute,
int
second)
– Returns the date in the default time zone, constructed from the given components (year, month, day, etc.).

long toDateInMillis(int year,
int
month,
int
day,
int
hourOfDay,
int
minute,
int
second)
– Returns the date in the default time zone, constructed from the given components (year, month, day, etc.). Same as
toDate(int, int, int, int, int, int)
but returns the date as the number of milliseconds since January 1, 1970, 00:00:00 GMT.

double toModelTime(double value,
time unit constant)
– Converts the timeout (given as the number of specified time units) to the model time units. You define the timeout using the function arguments: first, you pass the value as the
value
argument, and then specify the time units using the second argument,
units. For example, the model time units in your model are minutes. In this case the function call
toModelTime(195, SECOND)
will return 195/60 = 3.25

double toTimeoutInCalendar(time unit constant, double amount)– Returns timeout in model time units, which equals to the given
amount
of specified time units from current model date, e.g.
toTimeoutInCalendar( DAY, 1 )
returns timeout in model time units from
date()
to the
date()
1 day.

double toTimeUnits( double modelTimeValue,
time unit constant
)– Converts the timeout (specified as the number of model time units) to the specified time units. For example, the model time units are minutes. In this case the function call
toTimeUnits(5.5, SECOND)
will return 5.5*60 = 330.