moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.23k stars 730 forks source link

How Can I Format Date do CRON #1476

Closed Ellathet closed 1 year ago

Ellathet commented 1 year ago

Hi, I've tried to format the DateTime of CRON syntax, to run a scheduler in nestjs, but only found this solution:

  private dateToCron(date: DateTime) {
    const second = date.minute;
    const minute = date.minute;
    const hour = date.hour;
    const dayOfMonth = date.day;
    const month = date.month;
    const dayOfWeek = date.weekday;

    return `${second} ${minute} ${hour} ${dayOfMonth} ${month} ${dayOfWeek}`;
  }

I din't like this soluction, because i need implement a new function. I tried use a .toFormat function, but I don't no to get a weekday.

DateTime.toFormat("ss mm hh DD MM ??")

This, ?? should be a week day, like a CRON syntax

If don't have that option, I can try to implement getting the day of the week by the parser, with PR.

diesieben07 commented 1 year ago

This seems like a pretty niche use-case to me, especially because there is no 1:1 match between CRON and any Luxon object. CRON describes a recurring schedule, none of the Luxon objects do this.

Additionally, there is no formatting option for everything imaginable, and neither does there need to be, in my opinion. Your dateToCron function is something that can perfectly well live in user code and doesn't need to be in Luxon.

icambron commented 1 year ago

Yeah, I think we'll pass on a cron feature for the the reasons @diesieben07 states

However, what we probably should add is a token for weekday number. So we'd take a PR for that. Feel free to open a new issue about that.