question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing string values and treating as dates is incorrect

See original GitHub issue

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:


"Country";"Product Family"
"Germany";"Aprobil P 0.1%"

conversion to workbook is the following:

var workbook = XLSX.read(csvData, {
                    type:'string',
                    dateNF: 'D-M-YYYY',
                    cellDates:true,
                    cellText:true,
                    cellNF: false,
                raw:false});

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:

{
      t: 'd',
      v: 'Sat Apr 01 2000 00:00:00 GMT+0300 (Eastern European Summer Time)',
      z:undefined
}

is there any way to cover this case? Could you pls assist, what to do.

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
SheetJSDevcommented, Sep 11, 2019

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 raw: true will give you the raw strings, suppressing any sort of value interpretation.

If you are interested in helping, the relevant function is fuzzydate. Google Docs / Office Online likely use a series of regular expressions based on some known formats like /\d{4}-\d{2}-\d{2}/ for yyyy-mm-dd and that might be a more sensible approach than using the generic Date constructor

2reactions
SheetJSDevcommented, Feb 11, 2022

Based on some testing, in the en-US locale, there appear to be no valid Excel dates that contain letters outside of the month short or long names (even full day names are not accepted). Looking at a number of the issues, a guard like

    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 lower_months is an array of the long names of months) would address many of the problems. The proposed “strict” mode is still needed for determining field values and matching formats that Chrome does not accept (for example, 1:23.04:56)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Date.parse give incorrect results? - Stack Overflow
Now date and time strings without a timezone use the host timezone offset (i.e. "local"). Confusingly, ISO 8601 date only forms are treated...
Read more >
6 Ways to Fix Dates Formatted as Text in Excel
6 ways to fix dates formatted ast text in Excel, plus a bonus technique using Power Query to fix various formats.
Read more >
Date incorrect parsing from Excel - CloverCARE Support
If I define metadata as Date, it reads the Date without additional value in the front. Strange - it reads the value as...
Read more >
Date.parse() - JavaScript - MDN Web Docs
Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN . However, invalid values ......
Read more >
Defeating Automatic Date Parsing in Excel - Causal
In summary, automatic date parsing can cause problems in Excel because dates can be interpreted incorrectly or converted to a different format. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found