node-schedule / node-schedule

A cron-like and not-cron-like job scheduler for Node.
MIT License
9.1k stars 513 forks source link

nextInvocation() return type is "CronDate" instead of a standard Date #436

Open KevinNovak opened 6 years ago

KevinNovak commented 6 years ago

I was attempting to format and print out the next invocation date of a job, and noticed that nextInvocation() returns a CronDate instead of a standard JavaScript Date, which makes it difficult to format without converting or including the cron-parser package . Unless there is a reason to return CronDate, I think it would be better for users to have a standard Date returned. (This might also apply to other methods too).

Example:

const schedule = require('node-schedule');
const moment = require('moment');

var job = schedule.scheduleJob('0 12 * * *', () => {
    console.log('Running...');
});

// Directly printing
var time = job.nextInvocation();
console.log('Example 1:', time);

// Attempting to format
var formattedTime = moment(time).format('YYYY-MM-DD HH:mm:ss');
console.log('Example 2:', formattedTime);

// Convert and then format (workaround)
var convertedTime = new Date(time);
var formattedConvertedTime = moment(convertedTime).format('YYYY-MM-DD HH:mm:ss');
console.log('Example 3:', formattedConvertedTime);

Output:

Example 1: CronDate { _date: moment("2018-04-29T12:00:00.000") }
Example 2: 2018-04-28 00:00:00
Example 3: 2018-04-29 12:00:00

Is there a reason we return a CronDate as opposed to a regular Date?

santigimeno commented 6 years ago

@KevinNovak I see what you're saying, let me think a bit about it. Anyway, you can always call toDate() on a CronDate to get the actual Date.

lostfictions commented 6 years ago

Just got bit by this as well! The return format doesn't bother me, but it would be nice if it was correctly documented -- the typings mark the return value of nextInvocation() as a Date too.

teolag commented 4 years ago

Any updates on this? Seems like the returned object is a Moment date. In typescript i have to cast it as any and then call .toDate() to get a real Date object

Haiyn commented 2 years ago

Just got bit by this as well! The return format doesn't bother me, but it would be nice if it was correctly documented -- the typings mark the return value of nextInvocation() as a Date too.

This! It cost me an hour to figure out why it is documented as Date but loses all time precision.