What is the best practice to read date type value?
See original GitHub issueWith cellDates
read option, xlsx library tries to convert date-type cell to js Date object.
However, it does not seem to respect date1904
property of sheet when constructing js Date object. https://github.com/SheetJS/js-xlsx/issues/126
const xlsx = require('xlsx');
const ws = xlsx.readFile('./excel_date.xlsx', {cellDates: true});
console.log('date1904:', ws.Workbook.WBProps.date1904);
const firstSheet = ws.Sheets[ws.SheetNames[0]];
console.log(xlsx.utils.sheet_to_json(firstSheet));
The above code with the attached excel file gives the following result:
date1904: true
[ { Date: 2014-12-30T14:59:08.000Z,
String: 'I am text',
number: 1 },
{ Date: '2019-01-01', String: 1, number: 2 },
{ Date: 2014-12-30T14:59:08.000Z, String: '3', number: 3 },
{ Date: 2014-12-30T14:59:08.000Z, String: 2, number: 4 } ]
I expected that the generated js Date objects are of ‘2019-01-01’, but they are skewed due to date1904
problem.
I converted all js Date values in my program.
But I think It would be better that the library do this magical conversion so that users do not need to consider date1904
anymore.
Am I missing useful option?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:18 (3 by maintainers)
Top Results From Across the Web
Demystifying DateTime Manipulation in JavaScript - Toptal
This in-depth guide to DateTime manipulation should help you understand the programming concepts and best practices relevant to time and date without having ......
Read more >Java Best Practice for Date Manipulation/Storage for ...
I have read all of the other Q/A about Date Manipulation ... have learned to use date-time types in your database to track...
Read more >9 Working with dates | The Epidemiologist R Handbook
The dmy() function flexibly converts date values supplied as day, then month, then year. # read date in day-month-year format dmy("11 10 2020")....
Read more >Excel Date and Time - Everything you need to know
Excel will automatically convert the format of date serial numbers to suit your system settings as long as it's one of the default...
Read more >A Guide to Handling Date and Time for Full Stack JavaScript ...
Formatting the date ... One way to format a date is to use the getter functions like getFullYear, getMonth, getDate, etc. For example,...
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
This thread has ballooned to include a general discussion about handling dates. Could we return to the original issue, namely that
readFile
doesn’t return correct data? As was described in the original issue, the library correctly identifies that the file is based on 1904 rather than 1900 but the dates in the result don’t reflect that. Surely this isn’t meant to work this way?@zoeesilcock There are two more problems. SSF module output, instead of JS native Date, is preferable to represent date when importing excel file.
TL;DR. I am using following workaround code.
Run above code with tz_test_dates.xlsx and will get following result:
tz_test_dates.xlsx preview:
2019-01-01 | 1960-01-01 | 1908-01-01 | | | – | – | – | – | – | – 2019-01-01 | 2019-03-01 | 2019-05-01 | 2019-07-01 | 2019-09-01 | 2019-11-01
Detail
sheet_to_json
uses following code to convert Excel date code to JS Date object.issue 1: precision bug. refer to #1470
On some countries, you may lose some time (in Korea, -52 sec when parsing). The problem is that
Date.getTimezoneOffset()
is not precise enough. (SheetJS/ssf#38)Run above code after setting computer’s time zone to Asia/Seoul (UTC+09:00) then will get:
Notice that 52 seconds error has gone, but ‘1960-01-01’ and ‘1908-01-01’ are not correctly parsed. It is due to following issue.
issue2: timezone offset is not constant within one time zone.
Noice that
dnthresh
depends on the timezone offset of CURRENT TIME. But on some countries, timezone offset changes (or have changed) over time. In Korea, it is GMT+09:00 now, but it was GMT+08:30 in 1960 and GMT+08:27 in 1908. In Los Angeles in US, it is GMT-08:00 in January and GMT-07:00 in October due to summer time. For these countries,dnthresh
should not be constant and we should consider time zone change. SSF module which is timezone-agnostic rescues us.Above code gives following result:
On LosAngeles timezone:
On Asia/Seoul timezone: