moment / luxon

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

Feature Request: Macro Tokens for All Formats and Export expandMacroTokens function #1638

Open hemanth307 opened 4 months ago

hemanth307 commented 4 months ago

I am currently using macro tokens for date formats, and I need to expand them for placeholders. However, I have encountered two issues:

(i) The expandMacroTokens functions are not exported, which makes it challenging to use them for custom formatting.

(ii) Not all Luxon formats have corresponding macro tokens (e.g., DATE_MED_WITH_WEEKDAY).

It would be extremely helpful if macro tokens could be added for every format, and if the expandMacroTokens functions could be exported for easier customization.

Thank you for considering this feature request.

diesieben07 commented 4 months ago

You can use DateTime.parseFormatForOpts to convert any formatting options (like DATE_MED_WITH_WEEKDAY) into a format string:

console.log(DateTime.parseFormatForOpts(DateTime.DATE_MED_WITH_WEEKDAY)); // outputs: EEE, MMM d, yyyyy

expandMacroTokens is available via DateTime.expandFormat:

console.log(DateTime.expandFormat('DDD')); // outputs: MMMM d, yyyyy

Let me know if this solves your use cases.

hemanth307 commented 4 months ago
  1. Thank You That Helped, but i also have a locale which might be changing the format so can i pass the locale with the macrotoken format and get the particular format.

  2. Is there a way i can get the format if i pass the JavaScript Date Object and the locale. I need format not the formatted value.

  3. Can we get a macro option for DATE_MED_WITH_WEEKDAY which can change with the locale provided like for other tokens.

diesieben07 commented 4 months ago
  1. Both parseFormatForOpts and expandFormat accept a 2nd parameter "localeOpts", which lets you configure the locale. Specifically this accepts an object with keys locale (like "en-US"), numberingSystem, outputCalendar and weekSettings.
  2. I'm not sure what you want to do here. How do you get a format from a Date object? A Date object is basically just a wrapper around a timestamp. What kind of format do you want to get?
  3. We can't add macro tokens for every conceivable format, at some point we'll run out of letters in the alphabet (many tokens already make no sense in terms of abbreviations). parseFormatForOpts should solve this for you by letting you expand DATE_MED_WITH_WEEKDAY manually.