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

[0.15.1] Text being converted to dates #1636

Open
Webifi opened this issue Sep 21, 2019 · 6 comments
Open

[0.15.1] Text being converted to dates #1636

Webifi opened this issue Sep 21, 2019 · 6 comments

Comments

@Webifi
Copy link

@Webifi Webifi commented Sep 21, 2019

When exporting a table with table_to_book, a cell with a text value of "Smith, Julie + 5" is being converted to "07/05/2001". {raw: true} fixes this, but then columns with actual dates are not being converted.

Edit:
Looks like the issue is with the fuzzydate method.

While it won't fix everything (fuzzydate is way too, well, fuzzy), changing:

if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;

to:

if(s.match(/\b(jan(\b|uary\b)|feb(\b|ruary\b)|mar(\b|ch\b)|apr(\b|il\b)|may\b|jun(\b|e\b)|jul(\b|y\b)|aug(\b|ust\b)|sep(\b|tember\b)|oct(\b|ober\b)|nov(\b|ember\b)|dec(\b|ember\b))(\s*|\/|-)[0-9]+/i)) return o;
if(s.match(/[0-9]+(\b|st|th|\/|-)\s*\b(jan(\b|uary\b)|feb(\b|ruary\b)|mar(\b|ch\b)|apr(\b|il\b)|may\b|jun(\b|e\b)|jul(\b|y\b)|aug(\b|ust\b)|sep(\b|tember\b)|oct(\b|ober\b)|nov(\b|ember\b)|dec(\b|ember\b))/i)) return o;

would at least make the month match more restrictive.

Currently it looks like fuzzydate would match a date on text like "... You may need 5 ..." or "julie is missing 5 teeth" or "6 children marching" or even "I fear lauging clowns".

The updated regexps would require "may 5" or "jul 5" or "july 5th" or "6 march" (abbreviated or full month followed by a number or number (+optional ordinal) followed by month)

@Webifi Webifi changed the title Text being converted to dates [0.15.1] Text being converted to dates Sep 21, 2019
@CNzxy0820
Copy link

@CNzxy0820 CNzxy0820 commented Sep 28, 2019

May I request a question , whether the js-xlsx can show the excel datas use the format of "yyyy.m.d h:mm" not the "yyyy.m.d h:mm" to text format,when I used the js-xlsx , the "2019.9.22 16:22" was convert to "43730.682291666664",I want to add the datetime like "2019.9.22 16:22" to as ECharts'xaxis. I am sorry my english is poor ,very sorry... if you can answer my ,I will very very happy!!

@Webifi
Copy link
Author

@Webifi Webifi commented Sep 28, 2019

Pretty sure that's what the cellDates parse option is all about, but please move discussion for this elsewhere as it's unrelated to this issue.

@CNzxy0820
Copy link

@CNzxy0820 CNzxy0820 commented Sep 28, 2019

I am sorry, because I see your issue is relate to dates,so I want to request you... I am sorry to disturb you.. thank you for your answer.

@datermine
Copy link

@datermine datermine commented Nov 22, 2019

Related to this issue, I'm seeing this text: AUD 76.09 appearing as the date 8/31/76

@SheetJSDev
Copy link
Contributor

@SheetJSDev SheetJSDev commented Jun 27, 2020

All of these issues are related to date parsing.

@Webifi that is a good fix and we'll accept a PR for more restrictive date testing.

@CNzxy0820 to export a date using a specific format, set the "z" property of the cell to the desired format:

ws["A1"].z = 'yyyy"."m"."d h:mm'; // 2020.6.27 13:37
ws["A1"].z = 'yyyy"."mm"."dd hh:mm'; // 2020.06.27 13:37

@datermine related to dates but not related to this specific issue. The text "76.09" is parsed as a date (1976-09-01) but is not recognized as a number because of the prefix "AUD "

@subhamX
Copy link

@subhamX subhamX commented Oct 15, 2020

Hi @SheetJSDev, I would like to work on this issue. From the discussion, I think we wish to have more robust pattern checking before parsing a cell as Date.
One of the solutions, as shared by the author, is to perform these checks for any cell string:

if(s.match(/\b(jan(\b|uary\b)|feb(\b|ruary\b)|mar(\b|ch\b)|apr(\b|il\b)|may\b|jun(\b|e\b)|jul(\b|y\b)|aug(\b|ust\b)|sep(\b|tember\b)|oct(\b|ober\b)|nov(\b|ember\b)|dec(\b|ember\b))(\s*|\/|-)[0-9]+/i)) return o;
if(s.match(/[0-9]+(\b|st|th|\/|-)\s*\b(jan(\b|uary\b)|feb(\b|ruary\b)|mar(\b|ch\b)|apr(\b|il\b)|may\b|jun(\b|e\b)|jul(\b|y\b)|aug(\b|ust\b)|sep(\b|tember\b)|oct(\b|ober\b)|nov(\b|ember\b)|dec(\b|ember\b))/i)) return o;

I think it should be fine. But it doesn't take care of 10-05-2001 or 10/05/2001 etc. As these don't contain any month in letters it won't be parsed as a date. I suggest to add the following case too:

if(s.match(/(?:(?:31(\/|-| |\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-| |\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-| |\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-| |\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/) return o;

It will check for types like 31.01.20, 31.01.2020 etc. Should I make a PR with these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.