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.

Excel supports cells that display formatted dates. For testing purposes I created a file with only one cell (A1) and entered the value 2014-09-28. The value was immediately formatted as a date and displayed as 9/28/14.

I wanted to see how I can reliably extract back the original date. I used the following program:

var xlsx = require('xlsx');

var workbook = xlsx.readFile(process.argv[2]);
for (var i = 0; i < workbook.SheetNames.length; i++){
    var worksheet = workbook.Sheets[workbook.SheetNames[i]];
    console.log(worksheet);
}

And got the following output (after running it with node myfile.xlsx);

{ A1: { t: 'n', v: 41910, w: '9/28/14' }, '!ref': 'A1' }
{}
{}

I see several problems here:

  • The number 41910 is the number of days from some epoch, either 1900 or 1904, depending on where the file was created (Windows or Mac). There should be a way to tell how to treat this value.
  • The t field (for type) indicates this is a number. Can it instead indicate it’s a date? or should there be a different property?
  • There’s no way to retrieve the actual value that was entered - 2014-09-28 - so the original date, along with a reliable way of retrieving it, seems to be lost.

All of the above also applies for time fields.

Am I missing something? Is there a way to reliably get 2014-09-28 from the cell data?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:40 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
SheetJSDevcommented, Oct 4, 2014

My comment should not be construed to imply that I think any of this should be the “right” way to do things, and I’m sure we’ll come up with a better solution.

The t field (for type) indicates this is a number. Can it instead indicate it’s a date? or should there be a different property?

Quick review: the real thorn here is the bad date 2/29/1900 (1900 was not a leap year yet Excel recognizes it as a valid date). This is a bug from Lotus 1-2-3 that Excel replicated (decades later, it still haunts us). There is another issue regarding how to handle the day of week.

We could decide to throw away the corner case entirely and just store dates. In fact, the XLSX format allows for a cell type d for a date string. There would be problems at date code 60, but otherwise would be fine.

There should be a way to tell how to treat this value

The date1904 mode is stored in the (admittedly awkward) location Workbook.WBProps.date1904. Using the roo_190*_base files:

> require('xlsx').readFile('test_files/roo_1900_base.xlsx').Workbook.WBProps.date1904
'0'
> require('xlsx').readFile('test_files/roo_1904_base.xlsx').Workbook.WBProps.date1904
'1'

the original date, along with a reliable way of retrieving it, seems to be lost.

To convert the date, the SSF library has a handy parse_date_code function:

> require('xlsx').SSF.parse_date_code(41910, {date1904:false})
{ D: 41910,
  T: 0,
  u: 0,
  y: 2014,
  m: 9,
  d: 28,
  H: 0,
  M: 0,
  S: 0,
  q: 0 }
> require('xlsx').SSF.parse_date_code(41910, {date1904:true})
{ D: 41910,
  T: 0,
  u: 0,
  y: 2018,
  m: 9,
  d: 29,
  H: 0,
  M: 0,
  S: 0,
  q: 6 }

Those fields are described here: https://github.com/SheetJS/ssf/#usage

To recover a JS rendering of the date, take a look at the js-harb helper for this: https://github.com/SheetJS/js-harb/blob/master/bits/22_helpers.js#L7-L17

It probably makes sense for parse_date_code to return the original date object as well, as that would obviate the need for an additional function.

@hmalphettes @notatestuser @vratiu @wbrandongeorge thoughts?

1reaction
ligang-code2020commented, Aug 17, 2021

Now I want to turn off the date conversion. For example, I want to export the string ‘1.9.3’, and sheet.js is automatically installed and replaced with 2003/1/9. How to turn it off

well. i find way to resolve my question.just to set up {type:“array”,raw:“true”}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Insert the current date and time in a cell - Microsoft Support
To insert the current date and time, press Ctrl+; (semi-colon), then press Space, and then press Ctrl+Shift+; (semi-colon). Change the date or time...
Read more >
How to insert date in Excel: auto fill dates, enter today's date ...
Click on the cell with the first date to select it, and then drag the fill handle across or down the cells where...
Read more >
How to Make a Cell on Microsoft Excel With a Changing Date
Select the cell in which you want the current date to appear. Click the Formulas tab, then click Date & Time on the...
Read more >
Excel Split Date and Time Easy Formula or Flash Fill
How to get Date or Time value from a cell with simple Excel formulas. Use Flash Fill to split date and time without...
Read more >
Excel Date Functions - CustomGuide
You can use dates and time in your formulas just like any other value. For example, if cell A1 contained the entry 5/1/19...
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