There are javascript methods to get the day, month, year, etc from a UTC time.
BUT, if your goal is to run something at a specific time each day, then using a timer is overkill. It will run a task every minute (or every second) to simply check if it's the right time, and most of the time do nothing.
You can use asyncexec to schedule something to run at a specific time. Or more precisely, asyncexec runs something after a specific time interval. You just need to calculate what that interval is based on the current time and the time at which you want to run something. I've attached a panel to this post that I made for someone else. It runs something every day at 11:05:00.
In that panel, I have the following api code:
function runAtTime(time)
{
function recallMem()
{
ogscript.debug("DO MY TASK");
scheduleTomorrow(time); //Reschedule
}
var now = new Date();
var delay = time.getTime() - now.getTime();
if (delay < 0)
{
delay = 0;
}
ogscript.asyncExec(recallMem, delay);
}
This function runs the task at the scheduled time. The task is within the function, and it’s a function called recallMem. All it does it do a debug statement, and reschedule itself for tomorrow. You would replace that debug statement with something that calls your 3 tasks.
What we do, is we get the current time (called now). We calculate the difference between the time when we want to run it, and the current time. If it’s less than 0 (i.e. in the past), we make it 0.
Then we call asyncExec. This method runs a function, after a specific delay.
We also have the following 2 functions:
function scheduleToday(time)
{
var today = new Date();
var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
if (newTime.getTime() - today.getTime() < 0) //IF we are too late for today
{
ogscript.debug("Too late for today.");
scheduleTomorrow(time);
}
else
{
ogscript.debug("Scheduled task for today: " + newTime);
runAtTime(newTime);
}
}
function scheduleTomorrow(time)
{
var today = new Date();
var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
ogscript.debug("Scheduling for " + newTime);
runAtTime(newTime);
}
scheduleToday checks if the time we specified has already passed for today. If it’s in the future, we do the schedule. If it’s already passed, then we call scheduleTomorrow.
scheduleTomorrow calculates a date with the specified time, but for tomorrow, and schedules that task at that time.
Finally, we have something that schedules the task at a specific time (11:05:00.000):
var task1Time = new Date()
task1Time.setHours(11);
task1Time.setMinutes(5);
task1Time.setSeconds(0);
task1Time.setMilliseconds(0);
scheduleToday(task1Time);
You can change the 11 5 and 0 to be the exact time you want. You could also get those values from a parameter instead.
It’s a little more complex than the timers, but it’s much more precise and efficient.
If you still want to use timers, let me know, and I can cook up an example with timers. But the above is probably a better way to do it.
AtTime2.grid
#DashBoard