moment / luxon

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

add toLocaleString(presets_string) #1519

Closed MarcelFinkbeiner closed 9 months ago

MarcelFinkbeiner commented 9 months ago

Hello

Is your feature request related to a problem? Please describe. Currently, toLocaleString can only take a DateTime object as an argument. This can be limiting in cases where the date/time information is already available in string format and needs to be formatted according to a specific locale.

Describe the solution you'd like It would be helpful if toLocaleString could also accept a string as an argument, in addition to a DateTime object. This would allow users to format date/time information from a string using the same locale formatting options available with toLocaleString.

For example, instead of only being able to use dt.toLocaleString(DateTime.TIME_SIMPLE), I could use dt.toLocaleString('TIME_SIMPLE') to format a string containing date/time information.

Describe alternatives you've considered One alternative solution would be to create a new DateTime object from the string and then pass it to toLocaleString. However, this adds unnecessary complexity and could result in errors if the string is not formatted correctly.

Additional context N/A

Regards Marcel

diesieben07 commented 9 months ago

generated by chatgpt checked by human

I am sorry, but I am not sure what purpose this issue is supposed to have. The argument given hardly makes sense, which makes sense considering it was generated by an LLM. If you have a legitimate use-case, please elaborate what you want to achieve and why.

MarcelFinkbeiner commented 9 months ago

@diesieben07 what my problem is I want to pass the toLocaleString Presets as string not as an object?

Lets say I write an method getTime and I want to pass the format (Presets: https://moment.github.io/luxon/#/formatting?id=presets) as string, like 'DATE_SHORT' instead of import DateTime everytime.

And instead of using this string with an if else chain or an switch I'd like to pass it directly to the toLocaleString method of luxon/DateTime

diesieben07 commented 9 months ago

You can simply use a normal JavaScript property lookup:

import {DateTime} from 'luxon';

const formatPreset = 'DATE_SHORT'; // get from wherever instead of hardcoding
const date = DateTime.now();
console.log(date.toLocaleString(DateTime[formatPreset]));
MarcelFinkbeiner commented 9 months ago

You can simply use a normal JavaScript property lookup:

import {DateTime} from 'luxon';

const formatPreset = 'DATE_SHORT'; // get from wherever instead of hardcoding
const date = DateTime.now();
console.log(date.toLocaleString(DateTime[formatPreset]));

But this is the point I don't want to import luxon everytime. I have a util file which handles everything regarding luxon which I had to import. It would be a QOL feature ;)

diesieben07 commented 9 months ago

I don't think we should be adding features to reduce the amount of characters people need to type.

Personally I can just recommend to get a modern IDE, like Visual Studio Code (or WebStorm if you want to spend money), which will handle imports for you and can improve your productivity in many other ways as well. In my opinion this is a much more productive endeavor over trying to find ways to reduce how many imports you have to do manually.