Date cells
See original GitHub issueExcel 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:
- Created 9 years ago
- Comments:40 (23 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.The date1904 mode is stored in the (admittedly awkward) location Workbook.WBProps.date1904. Using the roo_190*_base files:
To convert the date, the SSF library has a handy parse_date_code function:
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?
well. i find way to resolve my question.just to set up {type:“array”,raw:“true”}