Fomantic-UI icon indicating copy to clipboard operation
Fomantic-UI copied to clipboard

[Calendar] wrong datetime parsing

Open prudho opened this issue 2 years ago • 4 comments

Bug Report

Actual develop branch seems to wrongly parse datetime in calendar module.

Culprit seems to come from this commit: 8a331f0e62806964cfa00c271b09d54076f28df5.

In src\definitions\modules\calendar.js at line 1563.

text = String(text).trim();

has been replaced by

text = String(text).replace(/\s/g,'');

When this line is reverted, date parsing is correct.

Steps to reproduce

  1. Create a minimal calendar.
  2. Set initial date by data-date="2022-03-17 09:35:00" or data-value="2022-03-17 09:35:00".
  3. Initialize the calendar module.

Expected result

March 17, 2022 9:35 AM

Actual result

March 1, 2022 5:35 AM

Testcase

JSFiddle

Screenshot (if possible)

image

Version

2.9@nightly

prudho avatar Mar 22 '22 09:03 prudho

Indeed... Reason for that was #2200 where whitespace was messing up the parsing "10. 6. 2021" I play around with a better regex instead of just removing any whitespace, because just reverting this line would again introduce the other bug. Thanks for pointing this out!

lubber-de avatar Mar 22 '22 09:03 lubber-de

If it helps, there are a bunch of different date formats here: https://en.wikipedia.org/wiki/Date_format_by_country

The trouble is supporting all of them could make the regex somewhat difficult

Thinking outside the box, could it be that an additional option is available for the calendar where an input date format is provided in order to parse the date into JavaScript correctly. Something like…

$(‘.ui.calendar’).calendar({
  format: “dd. mm. yyyy”,
});

This could then be used for time as well as datetime formats

Naturally the default format should probably be yyyy-mm-dd

It could even be that there is an inputFormat as well as an outputFormat (granted, formatter could be used for the output format, but this could be considered a far more simple approach if nothing fancy needs to happen in terms of logic/processes), where, by default, output matches input if output isn’t provided

jamessampford avatar Mar 22 '22 20:03 jamessampford

@prudho Fixed by https://github.com/fomantic/Fomantic-UI/pull/2275 as discussed (focussing on whitespace so the parser works again)

@jamessampford Regarding a general format (for output formatting), was requested in #568 already.

lubber-de avatar Mar 22 '22 21:03 lubber-de

@prudho Here is your fiddle using the patch https://jsfiddle.net/lubber/23y5mc0h/

lubber-de avatar Mar 22 '22 22:03 lubber-de