How do synced-cron job executes every 2 hour in a day. I'm using percolate:synced-cron package in meteor.
Here is my server code:
SyncedCron.add({
name: 'Cron job will start on',
schedule: function(parser) {
// parser is a later.parse object
return parser.text('every 2 hour');
},
job: function() {
FetchDailyBasisData();
}
});
Meteor.startup(function () {
SyncedCron.start();
});
It suppose to work but no luck.
It should be return parser.text('every 2 hours');
Notice the s in hours.
In the docs, the following periods are defined :
Periods
s, sec, seconds, m, min, minutes, h, hours, day, day of the month, day instance, day of the week, day of the year, week, week of the year, month, year
Related
**DATE FROM:**
def format=new java.text.SimpleDateFormat("yyyyMMdd")
def cal=Calendar.getInstance()
cal.get(Calendar.YEAR);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 31);
[format.format(cal.getTime())]
**DATE TO:**
def format=new java.text.SimpleDateFormat("yyyyMMdd")
def cal=Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH,-cal.get(Calendar.DAY_OF_MONTH))
[format.format(cal.getTime())]
when year changes (2020 - 2021) - it confuses January of previous year with January of this year
I have to correct so that in January (December reporting) it extracts data for period 31.01 - 31.12. of previous year.
The job was wrong because it extracted data from 31.01.2021 to 31.12.2020
// retrieve details of the current date
def cal = Calendar.instance;
def currentYear = cal.get(Calendar.YEAR);
def currentMonth = cal.get(Calendar.MONTH);
// set the instance to the start of the previous month
if ( currentMonth == 0 ) {
cal.set(currentYear-1, 11, 1);
} else {
cal.set(currentYear, (currentMonth-1), 1);
}
// extract the date, and format to a string
Date previousMonthStart = cal.time;
String previousMonthStartFormatted = previousMonthStart.format('yyyy-MM-dd');
If all you are looking for is the start of the previous year as in your title then the following code:
import java.time.*
def startOfPreviousYear = LocalDate.now()
.withDayOfMonth(1)
.withMonth(1)
.minusYears(1)
println startOfPreviousYear
def againStartingFromJanuary = LocalDate.of(2021, 1, 15)
.withDayOfMonth(1)
.withMonth(1)
.minusYears(1)
println againStartingFromJanuary
demonstrates one way to accomplish this. When run, this prints (with now being today's date of 2021.Mar.10):
─➤ groovy solution.groovy
2020-01-01
2020-01-01
updated after comments
You can get the end of previous and current months with something like this:
import java.time.*
def endOfPreviousMonth = LocalDate.now()
.withDayOfMonth(1)
.minusDays(1)
def endOfCurrentMonth = LocalDate.now()
.withDayOfMonth(1)
.plusMonths(1)
.minusDays(1)
println "end of last month: ${endOfPreviousMonth}"
println "end of current month: ${endOfCurrentMonth}"
which with current date prints:
end of last month: 2021-02-28
end of current month: 2021-03-31
or if we are in january:
def endOfPreviousMonth = LocalDate.of(2021, 1, 15)
.withDayOfMonth(1)
.minusDays(1)
def endOfCurrentMonth = LocalDate.of(2021, 1, 15)
.withDayOfMonth(1)
.plusMonths(1)
.minusDays(1)
println "end of last month: ${endOfPreviousMonth}"
println "end of current month: ${endOfCurrentMonth}"
which prints:
─➤ groovy solution.groovy
end of last month: 2020-12-31
end of current month: 2021-01-31
In general you should try to, when possible, stay away from using manual date arithmetic when dealing with dates if your target is based on the current date (as in, previous month, next month, three months ago, etc). Use the api:s handed to you by java. The date classes take care of rolling years, rolling months, rolling days, leap years, etc, all that stuff that you really do not want to spend time solving yourself.
I need to find if currentDate time (unix) and lastFetchedTime(unix) is greater than 30 minutes in moment.js.
How can compare the subtracted value from 30 minutes in moment?
lastFetchedTime(unix) is equivalent to the previous Date.now()..
const now = moment(Date.now());
const lastFetched = 1598578706;
const checkTime = now.diff(lastFetched, 'minutes') > 30 ;
You can use momentJS duration function to get the difference between two times which are in unix format.
Firstly, you need to convert the unix format to human readable time and then get the difference of current time and lastFetched time using asMinutes function of duration
If the difference is greater then 30 then do something else or do something else.
Live Demo:
const now = moment().unix()
const lastFetched = 1598597404;
const duration = moment.duration(moment.unix(now).diff(moment.unix(lastFetched)));
const getMinutes = duration.asMinutes();
if (getMinutes > 30) {
console.log('Minutes are GREATER then 30 minutes - from now')
} else {
console.log('Minutes are LESS then 30 minutes - from now')
}
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.js"></script>
Using JavaScript and moment library, given the user's now, number of hours and minutes needed to complete the job, and the following table of open and close hours at the shop in Seattle.
Working hours in Pacific time zone
I need to predict the date & time a job will be finished in the user's time zone by the shop in Seattle.
Can anyone show an example or suggest an algorithm to follow?
The programming langue does not matter, I can translate to JavaScript.
If there is an example using the 'moment' library it is even better.
Thank you much.
Examples:
If it is now Monday 8AM in PT and the job takes 3 hours, it will finish at Monday 11AM in PT because it does not hit a time that the shop is closed.
if the job takes 30 hours, then (22-8= 14 ) on Monday , 2+4 = 6 close time , (30-14) = 16 on Tuesday make the completion = Tuesday 4PM in PT (this to demonstrate just one closing time).
If the user is in NYC then his Monday 8AM , is 4AM in PT and there will be 22 hours to do the 30 hours job On Monday, 2+4 = 6 close time , (30-22) = 8 on Tuesday 8AM PT is Tuesday 4AM ET so for the NYC user, the completion is Tuesday 4AM.
Long jobs can traverse more than one closing time range and the weekend can add more.
We have users in all USA time zones and maybe soon in UK and DE but only one shop in Seattle
The algorithm you want to use is subtraction. Start with a list of duration available for each day. Keep a variable with the duration remaining. Find the number of hours remaining on the starting date and subtract that, or the duration remaining itself from the duration remaining - whichever is less. Repeat for subsequent days until the duration remaining is zero. If say on the last day there are still 4 hours remaining in their working day then subtract those 4 hours from their end of day to get the time they will finish the job.
It'll probably help to break everything down to integer milliseconds. Leave the conversion to the timestamp until the end.
I think I got it.
Who cares to check my logic Using Luxon.JS?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Job Done with LuxonJS</title>
<meta name="viewport" content="width=device-width">
<style>
#output {
font-size: 2rem;
}
#output2 {
font-size: 2rem;
/* color: red; */
}
#output3 {
font-size: 2rem;
color: green;
}
</style>
</head>
<body>
<header>
<h1>Calculating Completion Date Time</h1>
</header>
<main>
<div id="output"></div>
<div id="output2"></div>
<div id="output3"></div>
</main>
<!-- <script src="luxon.min.js"></script>-->
<script src="https://cdn.jsdelivr.net/npm/luxon#1.24.1/build/global/luxon.min.js"></script>
<script>
Number.prototype.between = function(a, b, inclusive) { //For elegant If statment
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return inclusive ? this >= min && this <= max : this > min && this < max;
};
console.clear();
//weekday: number Get the day of the week. 1 is Monday and 7 is Sunday
var OpenClose = [{}, // elemnt 0 not used to make Luxon Day Of the Week the index into the array
{
dw: 'Monday',
open: 4,
close: 22,
LuxonDayOfWeek: 1
},
{
dw: 'Tuesday',
open: 4,
close: 22,
LuxonDayOfWeek: 2
},
{
dw: 'Wedneday',
open: 4,
close: 22,
LuxonDayOfWeek: 3
},
{
dw: 'Thursday',
open: 4,
close: 22,
LuxonDayOfWeek: 4
},
{
dw: 'Friday',
open: 4,
close: 19,
LuxonDayOfWeek: 5
},
{
dw: 'Saturday',
open: 6,
close: 18,
LuxonDayOfWeek: 6
},
{
dw: 'Sunday',
open: 13,
close: 22,
LuxonDayOfWeek: 7
}
]; //orderby
console.log(OpenClose); //Show the Shop Schedule
let DateTime = luxon.DateTime;
var local = DateTime.local(); //Now here
//Make changes here !!
let Remainghours = 12.15; //Time alotted for the job. Change to test other values.
var NowInPT = local.setZone("America/Los_Angeles"); //Seattle is in this TimeZone
let JobDonePT = NowInPT; //Start at same time
output.textContent = "Starting in PT on " + NowInPT.toLocaleString(DateTime.DATETIME_SHORT) + " Job takes " + Remainghours + " hours";
while (Remainghours > 0) {
if (JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) {
console.log("Shop Open: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
Remainghours--; //Shop is open Use up an hour
} else {
console.log("Shop Closed: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
//keep going without Using Remainghours
}
JobDonePT = JobDonePT.plus({
hours: 1
}); //advance 1 hour on the calendar in any case
}
// Now we are left with a Negative fraction of an hour so we set set the actual Completion
JobDonePT = JobDonePT.plus({
hours: Remainghours
}); //Remainghours is negative
//The end DateTime may still be in Off hours Must end In work hours so
while (!JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) { //Not working hours
debugger;
JobDonePT = JobDonePT.plus({
hours: 1
});
};
output2.textContent = "Job End in PT on " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT);
var JobDoneMylocal = JobDonePT.toLocal();
output3.textContent = "Job End in My Time Zone on " + JobDoneMylocal.toLocaleString(DateTime.DATETIME_SHORT);
</script>
</body>
</html>
I am trying to get week start as friday & end date as friday and I tried to use startOf/endOf week(week/isoweek) but failed. Is there any way that I can get friday as start of week and Friday as end of week using moment.
Moment(date).startOf('week'); // or isoweek
Output should be,
Date of friday
Request data:
First date= 05-09-2019
End date= 05-15-2019(current date)
Expected output:
[
{
Weekstart: 05-03-2019,
Weekend: 05-10-2019
},
{
Weekstart: 05-10-2019,
Weekend: 05-17-2019
}
]
There is no option for setting start day of week . But you can fetch last Friday date using
moment().weekday(-2).format("YYYY-DD-MM")
You can update the week start for a locale using something like:
moment.updateLocale(moment.locale(), { week: { dow: 5 } })
moment().startOf('week').toString(); // Fri May 10 2019 00:00:00 GMT+0100
You can do a short custom function to always give you start and end of the week based on a passed isoWeekDay:
let getCustomWeek = (dayOfWeek=7, date=new Date()) => {
let firstDay, lastDay, passedDay = moment(date).isoWeekday(dayOfWeek)
firstDay = moment(passedDay).subtract(1, 'week')
lastDay = moment(firstDay).add(1, 'week')
return { start: firstDay.format(), end: lastDay.format() }
}
let dateRange = ['05-09-2019', '05-15-2019']
console.log('Friday: ', dateRange.map(d => getCustomWeek(5, new Date(d))))
console.log('Wednesday: ', dateRange.map(d => getCustomWeek(3, new Date(d))))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
I'm trying to get the difference between 2 dates in days, hours, and seconds:
import groovy.time.*
Date now = new Date()
// Using deprecated constructor just for this example
Date newYearsDay2000 = new Date(2000, 0, 1)
use (TimeCategory) {
now - newYearsDay2000
}
This prints:
-690023 days, -14 hours, -38 minutes, -27.182 seconds
Which is obviously nothing like the difference between today's date and 2000/1/1, where am I going wrong?
Thanks,
Don
Could be an issue with the deprecated constructor?
If you use Calendar (and the Groovy updated method) to create the newYearsDay2000 var, you get:
import groovy.time.*
import static java.util.Calendar.*
Date now = new Date()
// Use the static imported Calendar class
Date newYearsDay2000 = instance.updated( year:2000, month:JANUARY, day:1 ).time
use( TimeCategory ) {
now - newYearsDay2000
}
which gives the result:
3925 days, 23 hours, 59 minutes, 59.999 seconds
Edit
Yeah, the JavaDoc for Date shows that constructor with the comment:
Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Which leads me to believe that:
Date newYearsDay2000 = new Date(2000, 0, 1)
Is actualy creating the Date for new Years Day in the year 3900
Date
Parameters:
year - the year minus 1900.