Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[scope] date format on calendar component #3116

Open
batata004 opened this issue Nov 7, 2024 · 1 comment
Open

[scope] date format on calendar component #3116

batata004 opened this issue Nov 7, 2024 · 1 comment
Labels
type/usage Any support issues asking for help

Comments

@batata004
Copy link

Hi,

The calendar component is awesome however I am having a hard time dealing with different date formats. In Brazil, the default date format is day (2 digits) / month (2 digits) / year (4 digits). Fomantic UI makes it pretty easy to change the format, I just need to use:

	formatter:{
		
		date:'DD/MM/YYYY'
	
	},

It works great. However when I set the date with JS using set date or when I use the HTML attribute data-date, if I use the date in brazilian format, the component gets crazy (it outputs dates with month/day switched)... So I ask you this: when using the formatter it only applies to what the user sees visibly in the input and I should use the american format (yyyy-mm-dd) when using using set date with JS or when setting the HTML attribute data-date?

I think it would be better for this component to honor the format the user specified in the formatter in such a way that all the other configurations/parameters passed to the calender component should be treated as if in the format specified in the formatter. If this is no the case, the programmer has to deal with at least 2 different date formats all the time: one for the user visually and another for dealing with the component. Or am I saying something wrong?

I think at least the documentation should specificy the date format that the user has to use to deal with the component, I cant find it nowhere saying that should be YYYY-MM-DD.

@batata004 batata004 added state/awaiting-investigation Anything which needs more investigation state/awaiting-triage Any issues or pull requests which haven't yet been triaged type/bug Any issue which is a bug or PR which fixes a bug labels Nov 7, 2024
@lubber-de
Copy link
Member

lubber-de commented Nov 7, 2024

try to use monthFirst: false
See https://jsfiddle.net/lubber/oLutpay9/4/

The formatter option is infact only used to format the output wherever the date is shown somewhere in the component.
While it seems logical at first to also use the formatter string as input format, it could get quite complex as the formatter tokens support much more than just Y,M,D (see https://fomantic-ui.com/modules/calendar.html#custom-format)

The default date parser is already very complex and tries its best to get a given date string being parsed correctly
If the above suggested init option monthFirst (which the default parser supports) does not work for you, you are also able to provide your own date parser function like this

$('.ui.calendar').calendar({
    parser: {
        date: function(text) {
                if (text instanceof Date) {
                    return text;
                }
// always assume DD/MM/YYYY
                var day = text.substr(0,2),
                    month = text.substr(3,2),
                    year = text.substr(6,4);
                return new Date(year, month - 1, day, 0, 0);
        }
    }
});

@lubber-de lubber-de added type/usage Any support issues asking for help and removed state/awaiting-investigation Anything which needs more investigation state/awaiting-triage Any issues or pull requests which haven't yet been triaged type/bug Any issue which is a bug or PR which fixes a bug labels Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/usage Any support issues asking for help
Projects
None yet
Development

No branches or pull requests

2 participants