How can I run cronJob after every 31 second? - javascript

I want to run cronjob after every 31 seconds but it always runs twice in a minute like when the minute begins and after 31 seconds.
Output of my code:
Sat, 21 Jan 2023 17:58:00 GMT
Sat, 21 Jan 2023 17:58:31 GMT
Sat, 21 Jan 2023 17:59:00 GMT
Sat, 21 Jan 2023 17:59:31 GMT
Sat, 21 Jan 2023 18:00:00 GMT
Sat, 21 Jan 2023 18:00:31 GMT
But I want to run it after 31 seconds.
What I'm expecting:
`
Sat, 21 Jan 2023 17:58:00 GMT
Sat, 21 Jan 2023 17:58:31 GMT
Sat, 21 Jan 2023 17:59:02 GMT
Sat, 21 Jan 2023 17:59:33 GMT
Sat, 21 Jan 2023 18:00:04 GMT
Sat, 21 Jan 2023 18:00:35 GMT
Here is the Code:
const { CronJob } = require('cron')
new CronJob(
'*/31 * * * * *',
() => console.log(new Date().toUTCString()),
null,
true,
'UTC'
)
`

Try this:
31 * * ? * *
It means every minute at 31 seconds

Related

Date Disappearing from array

So I have this problem right now. I'm writing a mobile app through react native.
The problem is I'm trying to manipulate an array and it seems that a date drops from it for no reason a some point. here is the code and the output that it produces:
const res = labelDates.map((val) => {
const date = new Date();
date.setTime(0);
date.setFullYear(val.date.getFullYear());
date.setMonth(val.date.getMonth());
date.setDate(val.date.getDate());
console.log('mapping ' + val.date + ' to ' + date);
return date;
});
console.log(res);
And here is the output
mapping Thu Sep 03 2020 16:25:36 GMT+0200 (CEST) to Thu Sep 03 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 04 2020 16:28:40 GMT+0200 (CEST) to Fri Sep 04 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 05 2020 12:48:36 GMT+0200 (CEST) to Sat Sep 05 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 06 2020 14:28:56 GMT+0200 (CEST) to Sun Sep 06 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 07 2020 11:28:16 GMT+0200 (CEST) to Mon Sep 07 2020 01:00:00 GMT+0200 (CEST)
mapping Tue Sep 08 2020 18:22:36 GMT+0200 (CEST) to Tue Sep 08 2020 01:00:00 GMT+0200 (CEST)
mapping Thu Sep 10 2020 00:18:36 GMT+0200 (CEST) to Thu Sep 10 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 11 2020 00:18:36 GMT+0200 (CEST) to Fri Sep 11 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 12 2020 00:18:36 GMT+0200 (CEST) to Sat Sep 12 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 13 2020 00:18:36 GMT+0200 (CEST) to Sun Sep 13 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 14 2020 00:18:36 GMT+0200 (CEST) to Mon Sep 14 2020 01:00:00 GMT+0200 (CEST)
mapping Tue Sep 15 2020 00:18:36 GMT+0200 (CEST) to Tue Sep 15 2020 01:00:00 GMT+0200 (CEST)
mapping Wed Sep 16 2020 00:18:36 GMT+0200 (CEST) to Wed Sep 16 2020 01:00:00 GMT+0200 (CEST)
mapping Thu Sep 17 2020 00:18:36 GMT+0200 (CEST) to Thu Sep 17 2020 01:00:00 GMT+0200 (CEST)
mapping Fri Sep 18 2020 00:18:36 GMT+0200 (CEST) to Fri Sep 18 2020 01:00:00 GMT+0200 (CEST)
mapping Sat Sep 19 2020 00:18:36 GMT+0200 (CEST) to Sat Sep 19 2020 01:00:00 GMT+0200 (CEST)
mapping Sun Sep 20 2020 00:18:36 GMT+0200 (CEST) to Sun Sep 20 2020 01:00:00 GMT+0200 (CEST)
mapping Mon Sep 21 2020 00:18:36 GMT+0200 (CEST) to Mon Sep 21 2020 01:00:00 GMT+0200 (CEST)
Array [
2020-09-02T23:00:00.000Z,
2020-09-03T23:00:00.000Z,
2020-09-04T23:00:00.000Z,
2020-09-05T23:00:00.000Z,
2020-09-06T23:00:00.000Z,
2020-09-07T23:00:00.000Z,
2020-09-09T23:00:00.000Z,
2020-09-10T23:00:00.000Z,
2020-09-11T23:00:00.000Z,
2020-09-12T23:00:00.000Z,
2020-09-13T23:00:00.000Z,
2020-09-14T23:00:00.000Z,
2020-09-15T23:00:00.000Z,
2020-09-16T23:00:00.000Z,
2020-09-17T23:00:00.000Z,
2020-09-18T23:00:00.000Z,
2020-09-19T23:00:00.000Z,
2020-09-20T23:00:00.000Z,
]
As you see during the map call the array is containing the date 2020-09-08, but right after it terminates, my res array has dropped it for no reason!
I am sure data doesn't change since it's hardcoded and there is no other thread handling it.

Check if Date exist in array of Object

I have an array of date i want to sort it and get only the recent Date
[
"Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"
]
shouldDisplayDate(date: Date) {
datesFiltered = [];
const array = this.users.map(a => a.date)
for (const date of array) {
if (!this.datesFiltered.find(d => new Date(d).setHours(0, 0, 0) ===
new Date(dateString).setHours(0, 0, 0))) {
this.datesFiltered.push(new Date(dateString).toString())
}
}
}
Result :
[
Mon Jul 16 2018 15:32:50 GMT+0200 (Central European Summer Time),
Fri Jul 13 2018 09:33:46 GMT+0200 (Central European Summer Time),
Thu Jul 12 2018 13:41:59 GMT+0200 (Central European Summer Time)
]
So I want to check if I enter Mon Jul 16 2018 11:40:28 GMT+0200 (CEST) is in the Array of Object or not?
To see if two dates are equal using === you'll have to use .getTime() on them first (see this answer for more info)
An example of doing a simple date sort and finding if a date exists in your array are below.
To check if the date exists in the array I first convert a string date (the one we're searching for existence) to a Date obj. Then I loop through the array of existing dates and convert them one at a time and use Date.getTime() on each date to see if they are equal, if so, the function will return true.
const dates = [
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)"
];
// simple date sort
let sortedDates = dates.sort(function(a, b) {
return new Date(b) - new Date(a);
});
console.log(`sorted Dates array is: ${JSON.stringify(sortedDates, null, 2)}`);
console.log(`date exists in array? ${isDateInArray("Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)")}`);
function isDateInArray(dateString) {
let dateExists = false;
let date = new Date(dateString);
dates.forEach(function(arrayDateString) {
let arrayDate = new Date(arrayDateString);
if(date.getTime() === arrayDate.getTime()){
dateExists = true;
}
});
return dateExists;
}

Why this JavaScript date() is weird?

When I run the following JavaScript code it returns
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
Here I understand months are zero based in Date() so it overflows to July. But when I run following
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Here why the date is Jan 31 instead of throwing an exception?
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
Thanks
new Date(2017, 5, 31)
// Sat Jul 01 2017 00:00:00 GMT+0530 (IST)
June has only 30 days, so the balance 1 day (31 - 30 = 1) overflow to become July 01.
new Date(2017, 12, 31)
// Wed Jan 31 2018 00:00:00 GMT+0530 (IST)
Similarly, year 2017 has only 12 months, so the balance 1 month overflow to become 2018 Jan. Coincidently, January has 31 days too, so it becomes 2018 Jan 31 (31 - 31 = 0).
new Date(2017, 13, 31)
// Sat Mar 03 2018 00:00:00 GMT+0530 (IST). Why Mar 03 instead of Mar 31?
By that logic, year 2017 has only 12 months, so the balance 2 months overflow to become 2018 February.
Unfortunately, Febraury of 2018 has only 28 days, so the balance 3 days (31 - 28 = 3) overflow to become March 03.

How to convert an array of UTC dates into milliseconds using JS/Jquery

I'm having an array of dates:
var ticks = [];
ticks = [Tue Jan 01 2013 00:00:00 GMT-0800 (PST), Fri Feb 01 2013 00:00:00 GMT-0800 (PST), Fri Mar 01 2013 00:00:00 GMT-0800 (PST), Mon Apr 01 2013 00:00:00 GMT-0700 (PDT), Wed May 01 2013 00:00:00 GMT-0700 (PDT), Sat Jun 01 2013 00:00:00 GMT-0700 (PDT), Mon Jul 01 2013 00:00:00 GMT-0700 (PDT), Thu Aug 01 2013 00:00:00 GMT-0700 (PDT), Sun Sep 01 2013 00:00:00 GMT-0700 (PDT)].
How can I convert the above array of dates into millisecond format:
something like below:
[1390000000000, 1395000000000, 1400000000000, 1405000000000, 1410000000000, 1415000000000]
(PS: I need this format as I have a function that takes this format to display the x-axis)
Any ideas on how this can be achieved? Thanks!!!!
You could simply map a new array from the date objects:
ticks.map(function ( value ) {
return value.getTime();
});
Assuming you will get the raw data in string format, or else you have a bit of todo
var ticks = ... // raw data
var t2 = [];
for ( var i = 0; i < ticks.length; ++i ) {
t2.push( new Date(ticks[i]).getTime() );
}

JS setDate() in loop

I want to call a function for each day within next two weeks from now and pass parameters such as day and month. I use this method: startDate.setDate(startDate.getDate() + 1) which I found here but it gets messy after first 6 steps of the loop
var date = new Date,
searchDate = new Date,
period = 14;
for(i = 1; i <= period; i++){
searchDate.setDate(date.getDate() + i);
// someFunction(searchDate.getDate(), searchDate.getMonth());
}
What console.log(i + ": " + searchDate) returns:
1: Tue Aug 27 2013 17:38:04 GMT+0200
2: Wed Aug 28 2013 17:38:04 GMT+0200
3: Thu Aug 29 2013 17:38:04 GMT+0200
4: Fri Aug 30 2013 17:38:04 GMT+0200
5: Sat Aug 31 2013 17:38:04 GMT+0200
6: Sun Sep 01 2013 17:38:04 GMT+0200 // so far so good
7: Thu Oct 03 2013 17:38:04 GMT+0200 // and after a week it's suddenly October
8: Sun Nov 03 2013 17:38:04 GMT+0100
9: Thu Dec 05 2013 17:38:04 GMT+0100
10: Sun Jan 05 2014 17:38:04 GMT+0100
11: Thu Feb 06 2014 17:38:04 GMT+0100
12: Mon Mar 10 2014 17:38:04 GMT+0100
13: Tue Apr 08 2014 17:38:04 GMT+0200
14: Sat May 10 2014 17:38:04 GMT+0200
Any clue on this?
What you want is :
searchDate.setTime(date.getTime() + (24 * 3600 * 1000 * i));
It adds time to the internal value (timestamp) of the date instead of adding it to the day.

Categories