Node-scheduler how to set recursive condition - javascript

I want to run a background script every day at midnight after every 10 minutes.
It should run on 00:10, 00:20, and so on
What I have tried is.
schedule.scheduleJob("1 0-23 * * *", async () => {
})
What I want is to find a way to start this job at midnight and should be run after each 10 minutes

In your case schedule must be: */10 * * * * – run every 10th minute.
I'm not sure what you mean by starting at midnight. When it should finish then?
Update based on comment:
If you want to run it during the interval of 12 PM to 10 AM you'll need to specify the hour parameter. And it'll look like this: */10 0-10 * * * - every 10th minute past every hour from 0 through 10
start at 00:00:00
then at 00:10:00
then at 00:20:00
...
finish at 10:00:00
Hint: you can use crontab.guru to play with Cron formats.

Related

How to schedule node-cron job every new month?

I am using node-cron package for scheduling node-cron jobs. I want to schedule an node-cron job which will run every new month.
for example:
My node-cron job should run at 1 September 2020 after that it should run at 1 October 2020 and and so on..!
Please help me out for the above issue.
Thanks in advance.
I've tested accepted answer's code and noticed that there is something off.
cron.schedule(* * 1 * *) will make code run every first day of the month, and every hour and every minute. This means if it is first day of month, code will run once every minute.
To correct this issue (and actually run once a month, not multiple times in one day) we change: cron.schedule(* * 1 * *) to: cron.schedule(0 0 1 * *) so code runs every first day, at 00:00.
Following this tutorial I believe you just have to do:
const cron = require("node-cron");
cron.schedule("* * 1 * *", function() {
// Do something
});
where:
cron.schedule("* * * 1 * *", function() { // Do something });
OR
cron.schedule("* * 1 * *", function() { // Do something });
NOTE : remember that the first * of the six * is optional

How to execute a function for each period of time?

I want a certain function to run every round hour. There is the solution of running an interval when it's a round hour but I often turn on and off my script and I don't want to have to run it exactly on a round hour.
I've tried looking through some npm modules and I found one but I had some issues with it. Does anyone have a solution?
No need for javascript! You have the perfect tool for that if you use linux!
Use cron:
$ sudo crontab -e
This will open a vim editor. Then add:
0 * * * * node /execute/your/script.js
(basically, it will run your code every hour on its minute zero)
More info
cron: https://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
const HOUR = 1000 * 60 * 60;
function hourly() {
//....
setTimeout(hourly, HOUR);
}
setTimeout(hourly, HOUR - (new Date % HOUR));
Just calculate the next full hour when the server starts, and then shedule an hourly timer.
I admit that it might loose accuracy due to leap seconds :)

moment api method to calculate Today's time in milliseconds

var timeArr = moment().format('HH:mm').split(':');
var timeInMilliseconds = (timeArr[0] * 3600000) + (timeArr[1] * 60000);
This is my current solution. I'd rather use the moment api to calculate today's time (time since 12:00am today) in milliseconds.
My code returns today's time in milliseconds. I need to call another function in milliseconds. I can not use the epoch. I need today's time formated in milliseconds.
Examples:
9:00am = 3.24e+7 milliseconds
9:00pm = 6.84e+7 milliseconds.
You can try the following:
var moment = require("moment");
// Compute the time in milliseconds assuming it's 1AM now
moment("1:00","HH:mm")-moment("00:00","HH:mm")
// 3600000
// Compute the time in milliseconds assuming it's 9AM now
moment("9:00","HH:mm")-moment("00:00","HH:mm")
// 32400000
// Compute the time in milliseconds assuming it's 9PM now
moment("21:00","HH:mm")-moment("00:00","HH:mm")
// 75600000
// For current time
moment()-moment("00:00","HH:mm")
You can use the following functions:
moment() - this gives you the current moment ("now")
startOf('day') - this gives you the first moment of the day
diff - this calculates the difference between two moments
Putting it together:
moment().diff(moment().startOf('day'))
The default units are milliseconds, so you don't need to specify them.
This gives you the elapsed milliseconds since the start of the day. However, note that there are some time zones (Brazil, for example) where the DST spring-forward transition occurs right at the stroke of midnight - on such days, the clocks go from 23:59:59 to 01:00:00, so the first moment of the day might not be midnight! In that situation, values returned by the above function might appear to be off by an hour from what you expect. It is indeed returning time since the start of the day, just not time from "midnight".
To compensate for this possibility, you can do the following instead:
moment().utcOffset(0, true).diff(moment().utcOffset(0, true).startOf('day'))
The utcOffset(0, true) will flip to UTC mode while keeping the local time intact. This is a little more convoluted than the first example, but gets the job done because UTC never experiences DST transitions.

how to convert minutes to hours using moment.js

Can anyone tell me how to convert minutes to hours using moment.js and display in hh:mm A format.
For example,
If minutes is 480 it should display output as 08:00 AM.
If minutes is 1080 it should display output as 06:00 PM
Assuming that you always want to add minutes from midnight, the easiest thing to do is:
moment.utc().startOf('day').add(480, 'minutes').format('hh:mm A')
The use of UTC avoids issues with daylight saving time transitions that would cause the time to vary based on the day in question.
If you actually want the number of minutes after midnight on a given day, including the DST transitions take out the utc and just use:
moment().startOf('day').add(480, 'minutes').format('hh:mm A')
Note that the accepted answer has potential issues with DST transitions. For instance if you are in a part of the United States that observes DST:
moment('2016-03-13').hours(2).minutes(30).format('hh:mm A')
"03:30 AM"
The result is not as expected, and will vary between going back and hour or going forward an hour depending on the browser.
Edit: Original answer has been updated to fix bug. As an additional comment, I would be extremely leery of any code that attempts to map a number of minutes to civil time. The bottom line is that 480 minutes into the day is not always 8:00 AM. Consider this in the context of your problem. DST bugs are likely right now.
You can just do the basic arithmetic like so:
function getTimeFromMins(mins) {
// do not include the first validation check if you want, for example,
// getTimeFromMins(1530) to equal getTimeFromMins(90) (i.e. mins rollover)
if (mins >= 24 * 60 || mins < 0) {
throw new RangeError("Valid input should be greater than or equal to 0 and less than 1440.");
}
var h = mins / 60 | 0,
m = mins % 60 | 0;
return moment.utc().hours(h).minutes(m).format("hh:mm A");
}
getTimeFromMins(480); // returns "08:00 AM"
getTimeFromMins(520); // returns "08:40 AM"
getTimeFromMins(1080); // returns "06:00 PM"

Round time interval up to 15 minute steps

I have a scenario, where the user can push a button start a stopwatch and then push it again to stop it. But there's a twist - the end time needs to be rounded up in 15 minute steps. E.g. if the start time is 08:13 and the end time 08:16, it needs to be rounded up to 08:28. Or if the interval is longer than 15 minutes like 08:31, it needs to be rounded up to 08:43.
Do any of you have any pointers of how I could tackle this situation? If what I'm asking is too complicated, how do I round up and down to the closest 15 minutes (respectively).
This seems to be pretty simple:
var interval = 15 * 60 * 1000, // 15 minutes in miliseconds
roundedTime = new Date(startTime + (Math.ceil((endTime - startTime) / interval) * interval));
where startTime and endTime are Date objects.

Categories

Resources