-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Parsing string values and treating as dates is incorrect #1300
Comments
any update, any comments? is anybody here? ;) |
I am having a similar issue with certain strings being converted to date values. Any update on this? |
guys, @SheetJSDev, pls, just pay a little attention to it, tell us, is it possible to fix, or should we rely on our workaround? |
guys, one year has been passed, no one even commented. |
Hello @pzhelnov ! First, understand that this is an open source project and all work done on the project is charity. If you are concerned about response time or interested in supporting development, we offer paid builds and support as discussed in https://sheetjs.com/pro That said, unfortunately there's no magic solution here. The Date parsing currently falls back on the JS Date builtin features, which are super flexible in chrome. As an example: new Date("This is not a date 1") is the date January 1 2001, despite it clearly indicating that it isn't a date. Setting the option If you are interested in helping, the relevant function is |
Thank you very much for an answer! First of all I have to say this is great project, one of the best, flexible and configurable, which currently existing in the community. |
We'd accept a PR that does the following:
var STRICT_DATE_REGEXES = [
/\d{4}-\d{2}-\d{2}/
]
function fuzzydate(s/*:string*/, strict/*:boolean*/)/*:Date*/ {
var o = new Date(s), n = new Date(NaN);
if(strict) {
for(var i = 0; i < STRICT_DATE_REGEXES.length; ++i) if(s.match(STRICT_DATE_REGEXES[i])) return o;
return n;
}
var y = o.getYear(), m = o.getMonth(), d = o.getDate();
if(isNaN(d)) return n;
if(y < 0 || y > 8099) return n;
if((m > 0 || d > 1) && y != 101) return o;
if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
if(s.match(/[^-0-9:,\/\\]/)) return n;
return o;
} This obviously isn't 100% correct but is roughly shaped like how we'd expect the ultimate code to be. The new code doesn't the existing behavior (so we could get away with a patch version bump) but it does give you a backdoor to solve your immediate problem and gives us room to grow as we grind further |
Hey, is this issue still open to a PR? I'd like to give it a shot if I can |
As is usually the case with these problems, the code is the easy part. The hard part is making a decision :( If this is of interest, start by enumerating the common date formats that the parser should recognize (for example, |
Hmm, I see - let me start working on this in a bit and I'll see if I can add something of value that works. It's also my first open-source contrib, so please lmk if there's something more I can do! |
Hey! Just created a PR for this here - let me know if I need to make any more changes, etc. |
Based on some testing, in the var lower = s.toLowerCase();
if(lower.match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) {
lower = lower.replace(/[^a-z]/g,"");
if(lower.length > 3 && lower_months.indexOf(lower) == -1) return n;
return o;
} (where |
Fixed in 0.18.3: https://jsfiddle.net/9u84Lwja/ const csvData = `\
"Country";"Product Family"
"Germany";"Aprobil P 0.1%"`;
var workbook = XLSX.read(csvData, {
type:'string',
dateNF: 'D-M-YYYY',
cellDates:true,
cellText:true,
cellNF: false,
raw:false});
out.innerText = JSON.stringify(workbook.Sheets.Sheet1, 2, 2); The relevant cell is "B2": {
"t": "s",
"v": "Aprobil P 0.1%"
}, |
Hi!
There is a CSV file, which I try to read and it contains field with value
"Aprobil P 0.1%"
the short example of CSV:
conversion to workbook is the following:
after conversion I save the XLS, where value "Aprobil P 0.1%" is converted to a date 01.04.00
looking into the worksheet model and getting the certain cell, it contains:
is there any way to cover this case?
Could you pls assist, what to do.
Thanks in advance!
The text was updated successfully, but these errors were encountered: